Jump to content

an error plz help out here !!!!!!!!!!!!!!!!!!!!!!!!!!


webtuto

Recommended Posts

hi i dont knowwhy this douesnt succeed

<?php
include("config.php");
if($_POST['send']){
$query="select * from tuto";
$result=mysql_query($query);
while($row=mysql_fetch_array($result)) {
$id=$row[id];
}
$sql="insert into coms set `com`='$_POST[com]' , `tuto_id`='$id'";
$res=mysql_query($sql);
}

it gives the wrong tuto_id numbe

Link to comment
https://forums.phpfreaks.com/topic/85278-an-error-plz-help-out-here/
Share on other sites

Look at the order of your code

 

This executes a loop in the {  } brackets

 

while($row=mysql_fetch_array($result)) {

$id=$row[id];

}

 

So all it does is load $id with a $row[id], then repeats.  So when it gets to the last one it exits the loop then does the rest of your code.

 

Write down your logic on paper. I want to do this, this and this.  Then apply that logic to code.

Your code will bomb out if there are no results and this would cause $id to be not defined.

 

Also you have the wrong syntax. Are you inserting or updating. I commented on that part in the code.  You should look into that.

<?php
include("config.php");
if($_POST['send']){
$query="select * from tuto";
$result=mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($result)) {
$id=$row['id'];
}
// WRONG SYNTAX FOR INSERT. DO YOU MEAN UPDATE? //
$sql="update coms set com='{$_POST['com']}' , tuto_id='$id'";
$res=mysql_query($sql) or die(mysql_error());
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.