Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

insert into mysql 1

Status
Not open for further replies.

kzn

MIS
Jan 28, 2005
209
GB
Hi

I am struggling with entering data using php.

I have a query that pulls two rows of data from the database and displays the data in a table.

it creates the following html:
<form action="index.php" method="post">
<tr>
<td>789<input type="hidden" name="number[]" value="789" /></td>
<td>Telephone</td>
<td><textarea name="help[]" cols="30" rows="4"></textarea></td>
</tr>
<tr>
<td>790<input type="hidden" name="number[]" value="790" /></td>
<td>Computer</td>
<td><textarea name="help[]" cols="30" rows="4"></textarea></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Add" /><input type="hidden" name="submitted" value="true" /></td
<td>&nbsp;</td>

I want to be able to add both the help fields into the database with the respective number ie 789.

This is where i am lost, because we have got two arrays posted number[] and help[] and if i loop through one i cant get the corresponding value.

if(isset($_post['submitted'])) {

foreach($number as $n) {
$query = "insert $help into item where number = $n";
}
?>
 
inserts are for new records. how can you have a new record with a where clause?

updates and replaces are for changing records.

to obtain corresponding values

Code:
foreach ($_POST['number'] as $key=>$n){
  $h = $_POST['help'][$key];
}

and note that the insert syntax is

Code:
INSERT INTO table (col1, col2, ... coln) VALUES (val1, val2, ...valn)
 
my mistake it should have been an update query

but I still do not understand how i will make it update the two lines in the database;

$query = "update item set help = $help where number =789"; /// this would add the telephone

$query = "update item set help = $help where number =790"; // this would add the computer

I am totally lost.... sorry but im still learning. And thank you for your help
 
Code:
//use error management while debugging
ini_set('display_errors', 'on');
error_display(E_ALL);
///
foreach ($_POST['number'] as $key=>$n){
  $h = $_POST['help'][$key];
  $sql = update item set `help` = '".mysql_real_escape_string(trim($h)) . "' where number = " . intval($n);
  mysql_query($sql) or die(mysql_error());
}
 
Thank you so much for your help its working now.... could I ask would you mind explaining the line

$h = $_POST['help'][$key];

This goes right over the top of my head.

$key is index from the line
foreach ($_POST['number'] as $key=>$n){

Thanks again for all yor help
 
sure. you know that the help and number arrays are parallel so the key of one will correspond to the key of the other.
 
Thanks again. I really apppreciate your answers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top