Jump to content

[SOLVED] $_POST variable not updating mysql table


rubing

Recommended Posts

I am writing a script that presents an administrator with a page to update some of the system-level variables.  First, it checks to see if anything was submitted, if not it presents the option update values of variables.  If the admin submits (POST) these values the script then runs a mysql query to update them.  Unfortunately, the variables never update, they always stay the same. 

 

<?php
//if nothing was submitted present the updating form
if (!$_POST['submit']) {
include "dbinfo.inc";
$query= "SELECT * FROM program_setup";
$program_setup =mysql_query($query); 
$siteurl = mysql_result($program_setup,0,"siteurl");
echo '
<html><head></head><body>
<form action="admin_try1.php" method="post">
       <table>
      <tr> 
        <td>Name: siteurl   
Description: The complete directory URL to my program:  Example: ' . $siteurl . '<br />
            <input type="text" name="newsiteurl" size="60" value="' . $siteurl . '">
            </tr>
    <tr> 
        <td>  
          <input type="submit" name="submit" value="Update Setup Variables">
          </td>
      </tr>
    </table>
    
    </table>
</form></body>
</html>';

}
//else if something was submitted, update with the following query:
else {
include "dbinfo.inc";
$query="UPDATE program_setup SET siteurl={$_POST['newsiteurl']}";
mysql_query($query);
}
?>

Try this:

 

<?php
//if nothing was submitted present the updating form
if (!$_POST['submit']) {
include "dbinfo.inc";
$query= "SELECT * FROM program_setup";
$program_setup =mysql_query($query); 
$siteurl = mysql_result($program_setup,0,"siteurl");
echo '
<html><head></head><body>
<form action="admin_try1.php" method="post">
       <table>
      <tr> 
        <td>Name: siteurl   
Description: The complete directory URL to my program:  Example: ' . $siteurl . '<br />
            <input type="text" name="newsiteurl" size="60" value="' . $siteurl . '">
            </tr>
    <tr> 
        <td>  
          <input type="submit" name="submit" value="Update Setup Variables">
          </td>
      </tr>
    </table>
    
    </table>
</form></body>
</html>';

}
//else if something was submitted, update with the following query:
else {
include "dbinfo.inc";
$query="UPDATE `program_setup` SET `siteurl` = '".$_POST['newsiteurl']."'";
mysql_query($query);
}
?>

 

I changed the update query to use complete syntax.

You have this:

 

<?php
// Var needs to be cleaned as shown below
$newsiteurl = trim(mysql_real_escape_string($_POST['newsiteurl']));
$query="UPDATE `program_setup` SET `siteurl` = '$newsiteurl'";
// then just add
echo $query;die();
?>

 

This way we can make sure the data is ok.  Also you need to clean your post vars before running an update.

OK, so my code looks like this:

 

mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
$newsiteurl = trim(mysql_real_escape_string($_POST['newsiteurl']));
$query="UPDATE `program_setup` SET `siteurl` = '$newsiteurl'";
mysql_query($query);
// then just add
echo $query;
die();
}
?>

 

I run it and get:

 

UPDATE `program_setup` SET `siteurl` = 'http://www.pleasepleasepleaseupdate.com/'

 

However the value in the database for this cell does not change!!

Try displaying mysql errors:

 

<?php
mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
$newsiteurl = trim(mysql_real_escape_string($_POST['newsiteurl']));
$query="UPDATE `program_setup` SET `siteurl` = '$newsiteurl'";
$run = mysql_query($query);
if($run){
  echo $query;
  die();
}else{
   echo mysql_error();
   die();
}
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.