Forum Moderators: coopster

Message Too Old, No Replies

MySQL UPDATE and PHP problem

can't send php variable to MySQL DB

         

var123

5:41 pm on Apr 2, 2009 (gmt 0)

10+ Year Member



Hi, this is probably simple, I just can't figure it out. I have a table that is being populated from a database. However in the table there are some input boxes for where I want the user to input data which then I want to update my record in the database.

Here's an example :
Name: Joe
Address: 123 5th ST
Phone: <input from user>

Name: Kelly
Address: 555 Some Rd
Phone: <input from user>
<submit Button>

I have tested the sql query by using regular text instead of variables and it works. The problem is when I try to use variables from php like so:

// this one works fine
$sql = "UPDATE myTbl SET lName='Smith', fName='John' WHERE id=1";
$query = mysql_query($sql) or die(mysql_error());

// This is what I have
$sql = "UPDATE myTbl SET lName='{$lName}', fName='{$fName}' WHERE id=1";
$query = mysql_query($sql) or die(mysql_error());
[/code]
So that's where my error is. I have echo the $sql and its not getting any of the variable i enter in my form. Here is my full code:

<?
// first query for populating the table
$sql = "SELECT * FROM myTbl WHERE username='".$_SESSION['username']."'";
$result = mysql_query($sql) or die(mysql_error());

// start 2nd query for updating the record
if (isset($_POST['submit'])){

$lName = $_POST['lName'];
$fName = trim(mysql_prep($_POST['fName']));

$sql2 = "UPDATE myTbl SET lName='{$lName}', fName='{$fName}' WHERE id=4";

$result2 = mysql_query($sql2);
echo $sql2; // test query

} // end if isset
?>

<body>
<form method="post" action="<? $_SERVER['PHP_SELF'] ?>">
<table cellpadding="3" width="600px" border="1">
<tr>
<td>Address</td>
<td>City</td>
<td>Last Name</td>
<td>First Name</td>
</tr>

<?
while ($row = mysql_fetch_assoc($result)){
echo "<tr>
<td>".$row['address']."</td>
<td>".$row['city']."</td>
<td>".'<input type="text" name="lName" value="'.$lName.'">'."</td>
<td>".'<input type="text" name="fName" value="'.$fName.'">'."</td>
</tr>
?>

// I added the following line as a test to see if it would
// update my record and it did
<TR>
<TD><input type="text" name="fName" value="<? echo $fName ?>" /></TD>
</TR>
// but i dont want this in my form, just for testing

</table>
<input type="submit" name="submit" value="Submit" />
</form>
</body>

I would appreciate any and all help or ideas, thanks!

andrewsmd

6:26 pm on Apr 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



when you submit your form do a var_dump($_POST); and see what you get. If your variables are correct then they should show up the post hash

var123

7:48 pm on Apr 2, 2009 (gmt 0)

10+ Year Member



here is my output:
array(4) { ["lName"]=> string(0) "" ["fName"]=> string(0) "" ["clientEmail"]=> string(0) "" ["submit"]=> string(6) "Submit" }

so its not getting the variable values

andrewsmd

9:26 pm on Apr 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



got more input for you. Using the name as a name of your text box you are going to run into problems where names are the same i.e. john smith and bill smith are going to have the same post name $_POST['smith'] for their last name. Try this code you will have to modify a little bit

<html>
<?php

//just setting this for test data
$_SESSION['username'] = "username";

mysql_connect("localhost", "username", "password") or die(mysql_error());

mysql_select_db("my_database") or die(mysql_error());

// first query for populating the table
$sql = "SELECT * FROM test WHERE username='".$_SESSION['username']."';";
$result = mysql_query($sql) or die(mysql_error());

?>

<body>
<form method="post" name = "test">
<table cellpadding="3" width="600px" border="1">
<tr>
<td>Address</td>
<td>City</td>
<td>Last Name</td>
<td>First Name</td>
</tr>

<?php

//set the input boxes to their ids in the table

while ($row = mysql_fetch_assoc($result)){
echo("<tr>
<td>{$row['address']}</td>
<td>{$row['city']}</td>
<td><input type=\"text\" name=\"lN{$row['id']}\" value=\"{$lName}\"></td>
<td><input type=\"text\" name=\"fN{$row['id']}\" value=\"$fName\"></td>
</tr>");

}

?>

<TR>
<TD><input type="text" name="fName" value="<? echo $fName ?>" /></TD>
</TR>

</table>
<input type="submit" name="submit" value="Submit" />
</form>
</body>
<?php

// start 2nd query for updating the record
if (isset($_POST['submit'])){

$lName = $_POST['lName'];
//$fName = trim(mysql_prep($_POST['fName']));

$sql2 = "UPDATE myTbl SET lName='{$lName}', fName='{$fName}' WHERE id=4";

//$result2 = mysql_query($sql2);
echo $sql2; // test query
var_dump($_POST);
} // end if isset

?>
</html>

Play around with that.