Jump to content

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


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());
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.