I'm trying to make an page where you can select a value in a dropdown select box. When the selection is made it should update the database using the selected value. I use 2 pages one including the html/javascript and one using php. Currently when i select something nothing happens.
What am i doing wrong?
test.php
<html>
<head>
<script type="text/javascript" src="js/jquery.js"></script>
<script>
function updateDb() {
$.post("buh.php", $("#form").serialize());
}
</script>
</head>
<body>
<form id="form">
<?php
include 'Includes/database_connection.php';
$sql = "select * FROM sims ORDER BY phonenr asc" ;
$result = mysql_query($sql,$con);
echo "<select id='select' name='select' onChange='updateDb()'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['phonenr'] . "'>" . $row['phonenr'] . "</option>";
}
echo "</select>";
?>
</form>
</body>
And buh.php
<?php
include 'Includes/database_connection.php';
$sql = "select * FROM sims ORDER WHERE phonenr='".mysql_escape_string($_POST["select"])."'" ;
$result = mysql_query($sql,$con);
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
mysql_query("UPDATE pairings SET sim_id='$id' WHERE unit_id='1'")
or die(mysql_error());
}
?>
You have something wrong with you SELECT
query
$sql = "select * FROM sims ORDER WHERE phonenr='".mysql_escape_string($_POST["select"])."'" ;
^this ORDER here makes no sense
Either remove the word ORDER
wich is a clause to order by a column, or assign it a column to order by with the correct syntax
SELECT * FROM tablename WHERE yourcolumncondition ORDER BY yourcolumntoorderby
Then I would like to remember you that mysql_
functions are deprecated so i would advise you to switch to mysqli
or PDO
select * FROM sims ORDER ..You have added ORDER here wrongly in buh.php
$sql = "select * FROM sims ORDER WHERE phonenr='".mysql_escape_string($_POST["select"])."'" ;
$sql = "select * FROM sims ORDER WHERE phonenr='".mysql_escape_string($_POST["select"])."'" ;
remove order.