简体   繁体   中英

Unable to UPDATE record in Database from form

I have a form which i am posting data to a database. I want it to update the record where sku=sku, I have never done this before so i have read up on this and tried various ways but they have not worked. here is what i have tried so far.

<?php
if(isset($_POST['submit']))
{

$hostname_conn = "host address";
$database_conn = "database";
$username_conn = "username";
$password_conn = "password";
$conn = mysql_connect($hostname_conn, $username_conn, $password_conn) or trigger_error(mysql_error(),E_USER_ERROR); 

    mysql_select_db("bi_social", $conn);

    $sqlCmd = sprintf("UPDATE table2 SET sku2=%s sku=%s WHERE sku='%s'", 
    mysql_real_escape_string($_POST["sku"]),
    mysql_real_escape_string($_POST["sku2"]),
    mysql_real_escape_string($_POST["sku3"]));

    //echo $sqlCmd;
    //die();    

    mysql_query($sqlCmd);

    mysql_close($conn);
}

?>
<form method="post">
            <div class="large-3 columns">
<input name="sku" type="text" id="sku" placeholder="sku" value="<?=$_GET["sku"]?>"/>
            </div>
            <div class="large-3 columns">
<input name="sku2" type="text" id="sku2" placeholder="sku2" value="<?=$_GET["sku2"]?>"/>
            </div>
            <div class="large-3 columns">
<input name="sku3" type="text" id="sku3" placeholder="sku3" value="<?=$_GET["sku3"]?>"/>
            </div>
            <div class="large-3 columns">
    <input class="alert button" name="submit" type="submit" value="MATCH!"/>
    </div>
    </div>
    </div>
    </div>
</form>

I am not sure what i have done to make it not work i can though insert in to the database but updating does not work. Where am i going wrong and how do i fix this. Thanks Ryan

You are missing a comma in update statement. The update statement should look something like this:

UPDATE table2 SET sku2='%s', sku='%s' WHERE sku='%s'

Change:

$sqlCmd = sprintf("UPDATE table2 SET sku3=%s, sku2=%s WHERE sku='%s'", 
mysql_real_escape_string($_POST["sku3"]),
mysql_real_escape_string($_POST["sk2"]),
mysql_real_escape_string($_POST["sku"]));

OR,

$sqlCmd = "update table2 set sku3 = '".mysql_real_escape_string($_POST['sku3'])."', sku2 = '".mysql_real_escape_string($_POST['sku2'])."' where sku = '".mysql_real_escape_string($_POST['sku'])."'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM