Questions | Login

Top / PHP / MySQL

Syntax to REPLACE a record in a PHP/MySQL query?

demo 20 Dec 2008 06:06pm
I'm trying to set up a utility where the storeowner can choose from one of the specials she has previously entered into the database through an admin area.

I've got a dropdown menu that is populated with a field from one table. When selected, it should REPLACE the only record in another table. But nothing happens, and I get no warnings or errors.

I can do this through a static dropdown menu, but I want the menu to be generated dynamically.

If I code values in, the table is changed. But I don't seem to be able to figure out how to get my variables into that table.

This is the form:

<form name="form1" id="form1" action="" method="post">
<select name="special" id="special">

<?php
foreach ($conn->query($sql) as $row) {
?>
<option value="<?php echo $row['special']; ?>"><?php echo $row['title']; ?></option>

<?php } ?>

</select>
<input type="submit" name="edit" id="edit" value="Change now">
</form>

and this is the query:

if (array_key_exists('edit', $_POST)) {
// prepare update query
$sql = "REPLACE INTO special VALUES(1, 'placeholder')";
$stmt = $conn->prepare($sql);
// execute query by passing array of variables
$done = $stmt->execute(array($_POST['special']));
}
Where 1 is the record number I want to REPLACE and 'placeholder' represents the variable that will replace what is in the current table, if I knew how to express it properly.

Or should I be doing this a different way? I am using PDO, please no suggestions on mysql or mysqli. I don't know how to convert them and my server does not support them.

Answers

demo 20 Dec 2008 06:32pm
Hi Angela. First of all, congratulations for being a Top Contributor and for being so smart. I have read some of your answers and you are great. Now talking about your question, it is perfect that you have populated your drop-down menu with a field from one of your tables already.

You said: "I can do this through a static dropdown menu, but I want the menu to be generated dynamically." So Angela, what you need is AJAX. Considering that your SQL query does not have errors, all you need is AJAX, as I told you. Please look at this example:

http://www.w3schools.com/ajax/ajax_database.asp

Select a Name in the Box, and look what happens. Try to implement that. AJAX is the solution for what you need I think.

Related

Submit Answer

You must be logged in to post an answer. Please Login or Register.

Powered by phpMyAnswers.