Forum Moderators: coopster
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!
<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.