Advanced Search

Results 1 to 2 of 2

Thread: Simple form to update mysql

  1. #1
    Join Date
    Dec 2011
    Posts
    34
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Simple form to update mysql

    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 Code:
    <?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:
    HTML Code:
    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;  [B]//Set recordNum to the current row[/B]
    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?

  2. #2
    Join Date
    Jun 2012
    Posts
    12
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Hi,
    Try add recordNum into a hidden field, and add "name" attribute for "webURL".
    Code:
    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:
    Code:
    mysql_query("UPDATE adSources SET iconURL = '".$_POST['iconURL']."', webURL='".$_POST['webURL']."'
    WHERE adID=".$_POST['recordNum']);

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •