Page 1 of 1

Run SQL Update on button click in PHP

Posted: Wed Mar 25, 2009 6:26 pm
by srefer
Hi, I was wondering if it is possible to run an Update sql statement after a user clicks a button? For example, when a button is clicked, I want to run the following query:

mysql_query("UPDATE tech SET name='newName', email='newEmail', phone='newPhone', pass='NewPass' WHERE $usr_id = $row[techid];");

Is this possible, and is that how I get my input values to be used in the update?
My code is below:

<?php
$link = mysql_connect('localhost', 'root', 'root');
if (!$link){
die('Could not connect' . mysql_error());
}
echo 'Connected Successfully <br />';
$selectdb = mysql_select_db ('chfa', $link);
if(!$selectdb){
die('Could not select database' . mysql_error());
echo 'Did not select database';
}
echo 'Database Selected <br />';

$usr_id = $_GET["id"];
echo $usr_id;

$result = mysql_query("SELECT * FROM tech WHERE techid = '$usr_id';");

echo "<table border='1'>
<tr>
<th>Name</th>
<th>E-mail Address</th>
<th>Phone Number</th>
<th>Password</th>
</tr>";

while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td><input type='text' name='newName' value=" . $row['name'] . "/></td>";
echo "<td><input type='text' name='newEmail' value=" . $row['email'] . "></td>";
echo "<td><input type='text' name='newPhone' value=" . $row['phone'] . "></td>";
echo "<td><input type='text' name='newPass' value=" . $row['pass'] . "></td>";
echo "</tr>";
}

mysql_close($link)

?>

Re: Run SQL Update on button click in PHP

Posted: Wed Mar 25, 2009 6:37 pm
by waylon999
This can be done easily if you are not opposed to having the page reload. Just set the action of the button to a page that has your update code run. If you want this to happen without the page reloading then you can use javascript to call a php page on your server (ajax).

Which method are you going for?

Re: Run SQL Update on button click in PHP

Posted: Wed Mar 25, 2009 7:02 pm
by srefer
I think I would say reloading the page, I'd like to stick with just PHP and html. Do I have to put my input values in a form and use the post action then in order to get the values to run the update statement?

Re: Run SQL Update on button click in PHP

Posted: Wed Mar 25, 2009 7:25 pm
by waylon999
Do I have to put my input values in a form and use the post action then in order to get the values to run the update statement?
Yes exactly.