Need help to understand what is going wrong

edit.php

[php]
<?php
 
 // Connects to your Database 
 mysql_connect("mysql.mydomain.com", "username", "password") or die(mysql_error()); 
 mysql_select_db("my_database") or die(mysql_error()); 
 
$sql = "select * from record where ID = 1";
$query = mysql_query($sql);
 
while ($row = mysql_fetch_array($query)){
 
    $ID = $row['ID'];
    $nameTitlePrefix = $row['nameTitlePrefix'];
    $nameGiven = $row['nameGiven'];
    $nameFamily = $row['nameFamily'];
    $nameTitleSuffix = $row['nameTitleSuffix'];
  
}
mysql_free_result($query);
?>[/php]
 
<html>
<head>
<title>Edit Record</title>
</head>
 
<body>
 
<form action="update.php" method="post">
 
userid:<br/>
<input type="text" value="<?php echo $ID;?>" name="ID" disabled/>
 
<br/>
 
Title Prefix:<br/>
<input type="text" value="<?php echo $nameTitlePrefix;?>" name="nameTitlePrefix"/>
 
<br/>
 
Given Name:<br/>
<input type="text" value="<?php echo $nameGiven;?>" name="nameGiven"/>
 
<br/>
 
Family Name:<br/>
<input type="text" value="<?php echo $nameFamily;?>" name="nameFamily"/>
 
<br/>
 
Title Suffix:<br/>
<input type="text" value="<?php echo $nameTitleSuffix;?>" name="nameTitleSuffix"/>
 
<br/>
 
<input type="submit" value="save"/>
 
</form>
</body>
</html>

update.php

[code][php]

<?php // Connects to your Database mysql_connect("mysql.mydomain.com", "username", "password") or die(mysql_error()); mysql_select_db("my_database") or die(mysql_error()); $ID = $_POST['ID']; $nameTitlePrefix = $_POST['nameTitlePrefix']; $nameGiven = $_POST['nameGiven']; $nameFamily = $_POST['nameFamily']; $nameTitleSuffix = $_POST['nameTitleSuffix']; $sql = "UPDATE record SET nameTitlePrefix = '$nameTitlePrefix', nameGiven = '$nameGiven', nameFamily = '$nameFamily', nameTitleSuffix = '$nameTitleSuffix' WHERE record.ID = '$ID' LIMIT 1"; mysql_query($sql) or die ("Error: ".mysql_error()); echo "Database updated. Edit record"; ?>[/php][/code]

I am new to PHP/SQL & have come over from FMPro, really really loving this new found freedom!

Above is some code i have adapted from a tutorial i have read about submitting data to SQL via a PHP form.

Only problem is i can’t get it to work the way they have suggested.

if i replace the ‘$ID’ variable with ID in this part of update.php it works perfectly…

fails:

$sql = "UPDATE record SET nameTitlePrefix = '$nameTitlePrefix', nameGiven = '$nameGiven', nameFamily = '$nameFamily', nameTitleSuffix = '$nameTitleSuffix' WHERE record.ID = '$ID' LIMIT 1";

works:

$sql = "UPDATE record SET nameTitlePrefix = '$nameTitlePrefix', nameGiven = '$nameGiven', nameFamily = '$nameFamily', nameTitleSuffix = '$nameTitleSuffix' WHERE record.ID = ID LIMIT 1";

but i really need to know how to do this with the variable. (the tutorial did say this was a simplified version & did not allow for proper security & should really have a dynamic ID at the start of edit.php)

Any help in allowing me to understand the issue would be great

best

Stuart

Just to check, have you echoed out $ID before the SQL statement to ensure that you’re using the expected value?

Hi Smokey,

Thanks for the reply.

I am afraid that it is too early days for me to understand your question properly. … do you mean if i code a page that just prints the variable?

the code above edit.php & update.php contains everything.

best

Stuart

Ok, change this line:

[php]
$sql = “UPDATE record SET nameTitlePrefix = ‘$nameTitlePrefix’, nameGiven = ‘$nameGiven’, nameFamily = ‘$nameFamily’, nameTitleSuffix = ‘$nameTitleSuffix’ WHERE record.ID = ‘$ID’ LIMIT 1”;
[/php]

to this:

[php]echo ’ ID = [’.$ID.’]’;

$sql = “UPDATE record SET nameTitlePrefix = ‘$nameTitlePrefix’, nameGiven = ‘$nameGiven’, nameFamily = ‘$nameFamily’, nameTitleSuffix = ‘$nameTitleSuffix’ WHERE record.ID = ‘$ID’ LIMIT 1”; [/php]

This is to make sure $ID is containing what you’re expecting it to contain.

Sponsor our Newsletter | Privacy Policy | Terms of Service