0

I have a form that populates the fields with query results and allows me to update any field. My issue is that any given field will only show the first word in that column of the record. If I type something in the field it "does" update the record but only displays the first word. Here is what I have.

<html>
<body>
<?php
$con = mysql_connect("localhost","db_username","db_pw");
{
}
mysql_select_db("db_name",$con);
if (isset($_POST['update'])){
$UpdateQuery = "UPDATE db_table SET Name='$_POST[Name]', Email='$_POST[Email]', ID='$_POST[ID]', Notes='$_POST[Notes]' WHERE ID='$_POST[Hidden]'";
mysql_query($UpdateQuery, $con);


};

$sql = "SELECT * FROM db_table ";
$mydata = mysql_query($sql,$con);

echo "<table border='1'>
<tr>
<th>Name</th>
<th>Email</th>
<th>Member Number</th>
<th>Notes</th>
</tr>";

while($row = mysql_fetch_array($mydata))
{
echo "<form action=updaterec.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=text name=Name value=" . $row['Name'] . " ></td>";
echo "<td>" . "<input type=text name=Email value=" . $row['Email'] . " ></td>";
echo "<td>" . "<input type=text name=ID value=" . $row['ID'] . " ></td>";
echo "<td>" . "<input type=text name=Notes value=" . $row['Notes'] . " ></td>";
echo "<td>" . "<input type=hidden name=Hidden value=" . $row['ID'] . " ></td>";
echo "<td>" . "<input type=submit name=update value=update" . " ></td>";
echo "</form>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
2
  • 3
    Bobby tables is coming for you bobby-tables.com
    – miknik
    Oct 6, 2017 at 0:12
  • This is going to be in a password protect directory. I will be the only one using it.
    – PCMedicJAX
    Oct 6, 2017 at 2:34

1 Answer 1

0

The problem is because you're not wrapping the value in double quotes, your html is being generated as <input name=john smith> instead of <input name="john smith"> , which would wrap the whole value for your input field and then become valid.

So, because you're using double quotes, you need to use \"...

value=" . $row['Name'] . "

...should become...

value=\"" . $row['Name'] . "\"

You should always review your source code after PHP has generated in the browser (and not just the completed dom) for validity when having problems.

I'd also look at escaping your values.

Full Solution:

while($row = mysql_fetch_array($mydata))
{
    echo "<form action=updaterec.php method=post>";
    echo "<tr>";
    echo "<td><input type=text name=Name value=\"" . htmlentities($row['Name']) . "\" ></td>";
    echo "<td><input type=text name=Email value=\"" . htmlentities($row['Email']) . "\" ></td>";
    echo "<td><input type=text name=ID value=\"" . htmlentities($row['ID']) . "\" ></td>";
    echo "<td><input type=text name=Notes value=\"" . htmlentities($row['Notes']) . "\" ></td>";
    echo "<td><input type=hidden name=Hidden value=\"" . htmlentities($row['ID']) . "\" ></td>";
    echo "<td><input type=submit name=update value=\"update\" ></td>";
    echo "</form>";
    echo "</tr>";
}

Extra: In addition, single quotes can help to avoid confusion when you don't need to parse variables.

echo '<td><input type="text" name="Name" value="' . htmlentities($row['Name']) . '" ></td>';
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.