Jump to content

***Textfield SELECT INSERTION HELP

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
seyz4all

seyz4all

    Newbie

  • Members
  • PipPip
  • 13 posts
Please i need help on this, i am new with php, please can anyone help me with the code to this scenario...

I will appreciate

this is the case scenario of what i want to achieve

the first textfield would accept the id

and the user would click on the search button,

and a form with 3 textfields is generated with the result of the search in the textfields,

that is textfield 1 would display the id
textfield 2 would display the name
textfield 3 would display the age

and then i can now click on a submit button to take the data into another table in that database

#2
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Code for the search form:

<form action="search.php" method="post">
Id <input type="text" name="id" />
<p>
<input type="submit" value="Search" />
</p>

This file will send the POST data to search.php which will then connect to your database.

Search.php

<?php
// connect to database here

$sql = "SELECT * FROM age WHERE id='$_POST[id]'";
$result = mysql_query($sql);
?>
<form action="insert.php" method="post">
<?php
while ($row = mysql_fetch_array($result)) {
?>
id <input type="text" name="id" value="<?php echo $row['id'];?>" />
<br />
Name <input type="text" name="name" value="<?php echo $row['name'];?>" />
<br />
Age <input type="text" name="age" value="<?php echo $row['age'];?>" />
<?php
}
?>
<input type="submit" name="Insert" value="Insert" />
</form>

Then this file selects from the database, the record with the id that was supplied by the previous script. Then a loop through all the results and the values of each field is printed to the text box.

Then you are free to send this data off to another file. :)

Edited by chili5, 15 August 2008 - 05:33 AM.


#3
seyz4all

seyz4all

    Newbie

  • Members
  • PipPip
  • 13 posts
thanks alot, i give it a try, it looks really rite and good, i hope it works

#4
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
:D I tried it and it works. :) You might have to modify it to match your database a little. :)

#5
seyz4all

seyz4all

    Newbie

  • Members
  • PipPip
  • 13 posts
thanks man, it works...

on my insert button, what do i do to insert it to another table


is there anywhere one can add reps on this forum, i will definatley add reps for you....

#6
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Click Posted Image that image in my above post. It's just beside "permalink". :)

As for inserting data, yes. :) The file search.php is set up to send the POST data to insert.php. So insert.php

<?php
// database connection stuff

mysql_query("INSERT INTO history (id, name, age) VALUES('$_POST[id]','$_POST[name]','$_POST[age]')");

echo "Database updated.";
?>

Edit: ok "history" is the table name, then the things in brackets are the column names. Then the things after values are what you want to insert into the database. This have to be in single quotes for it to work. :)

:) To get a better idea this is the table I used:

CREATE TABLE IF NOT EXISTS `history` (
  `id` int(11) NOT NULL,
  `name` varchar(100) NOT NULL,
  `age` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

You might have to modify it to match your database better but it works. :) Hopefully that is clear, and you can modify the PHP above to match your database. :)

#7
seyz4all

seyz4all

    Newbie

  • Members
  • PipPip
  • 13 posts
dude u did it again...
thanx alot..
i'd give u more reps soon...

to believe this is my first thread and my problem was solved...

Chili5, much respect

#8
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Your welcome :D Need anything else just ask. :)