user1641165
user1641165

Reputation: 456

Update query in MySQL and PHP not working

PHP usually works pretty much straight out of the box, but I cannot get this query to work. I am attempting to update a simple record. My code is as follows.

<?php

 $customername=mysql_real_escape_string($_POST['customername']); 
 $contact=mysql_real_escape_string($_POST['contact']); 
 $customerorder=mysql_real_escape_string($_POST['customerorder']); 
 $orderplaced=mysql_real_escape_string($_POST['orderplaced']); 
 $staffmember=mysql_real_escape_string($_POST['staffmember']); 
 $daterequired=mysql_real_escape_string($_POST['daterequired']); 
 $status=mysql_real_escape_string($_POST['status']); 
 $paid=mysql_real_escape_string($_POST['paid']); 
 $delivery=mysql_real_escape_string($_POST['delivery']); 
 $ordercontent=mysql_real_escape_string($_POST['ordercontent']); 
 $orderid=mysql_real_escape_string($_GET['orderid']); 

mysql_connect("localhost", "xxxx", "xxxxxxx") or die("Connection Failed");
mysql_select_db("xxxxxx")or die("Connection Failed");


$query = "UPDATE ordermanager_orders SET customername='$customername', contact='$contact',  ordernumber='$customerorder', placedby='$orderplaced', requiredby='$daterequired', status=$status', staffmember='$staffmember', paid='paid', delivery='$delivery', ordercontent='$ordercontent' WHERE order='$orderid'";

if(mysql_query($query)){
 echo "updated";}
 else{
 echo "fail";}

?> 

The values are being posted or "Getted" from another page. They are definitely coming through as otherwise it comes up with errors. At the moment it simply comes up with 'failed' as per the code.

I have tried numerous variants of code found on the internet, to check if my coding was correct, however I still cannot get it to work.

Upvotes: 1

Views: 160

Answers (4)

Ben
Ben

Reputation: 11188

Your error is because of not handling your status update correctly: status=$status' must be status='$status'.

You would have figured this if you'd put a mysql_error() in your 'fail' section.

Upvotes: 1

Conrad Lotz
Conrad Lotz

Reputation: 8818

Try changing by adding a comma

status=$status' to status='$status'

And FYI change paid='paid' to paid='$paid' to ensure the correct value is passed

Upvotes: 1

Eric T
Eric T

Reputation: 1026

I wonder if u have tried to print_r your $query to check.

1st. Shift your connection string to the top most.

2nd. status=$status' <<=== less 1 quote

Upvotes: 4

472084
472084

Reputation: 17886

I thought you had to connect to the database before you were able to use mysql_real_escape_string()? Try connecting above all other code.

The status section of the query is also missing a quote mark.

Upvotes: 3

Related Questions