PDA

View Full Version : Simple form to update mysql



lmbarns
07-26-2012, 01:12 AM
I'm struggling to understand how you pass a js variable into the php file.

I have 2 php files. One selects a record and displays it in a table.

The second is an update.php that I want to use to pass info from my form to update a row in the DB.


<?php
$con = mysql_connect("localhost","username","mypass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("tableName", $con);

mysql_query("UPDATE adSources SET iconURL = '$_POST[iconURL]', webURL='$_POST[webURL]'
WHERE adID=recordNum");

mysql_close($con);
?>

That doesn't work, I'm not sure how to get recordNum, a javascript variable, into this sql statement. I'm not sure that the POST statements are correct either.

The Database table has only 3 attributes, adID, iconURL, webURL where adID is equal to recordNum, I want to update the other 2 attributes based on recordNum

Here's the js function for my form:


var recordNum = 0; //initialize it to 0 on start

function updateRecord(recordNum){
var myD = document.getElementById("updt");
myD.innerHTML = '<br />Update info for record #'+recordNum+'<br /><br /><form action="updatesql.php" method="post">Icon URL: <input type="text" name="iconURL" /><br />Web URL: <input type="text" id="webURL" /><br /><input type="submit" value="Update Record" /></form>';
}

function showUser(str)
{
recordNum = str; //Set recordNum to the current row
updateRecord(recordNum); //Call updateRecord which makes the input form that will update the entries
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}


<form>
<select name="adSources" onchange="showUser(this.value)">
<option value="">Select an ad unit:</option>
<option value="1">unit #1</option>
<option value="2">unit #2</option>
<option value="3">unit #3</option>
</select>
</form>


Here's where I'm at: http://simplehotkey.com/adverts/sql.html Select an item from the dropdown menu and it will load the record, I need to be able to submit data that will override the record with my new input.

Nothing fancy, it won't be public this is just for me to update some stuff.

Can anyone help clarify how I can do this?

CoursesWeb
07-26-2012, 12:44 PM
Hi,
Try add recordNum into a hidden field, and add "name" attribute for "webURL".

myD.innerHTML = '<br />Update info for record #'+recordNum+'<br /><br /><form action="updatesql.php" method="post"><input type="hidden" name="recordNum" value="'+ recordNum+ '" />Icon URL: <input type="text" name="iconURL" /><br />Web URL: <input type="text" name="webURL" id="webURL" /><br /><input type="submit" value="Update Record" /></form>';
Then, in ph0p:

mysql_query("UPDATE adSources SET iconURL = '".$_POST['iconURL']."', webURL='".$_POST['webURL']."'
WHERE adID=".$_POST['recordNum']);