Jump to content

Recommended Posts

Hi! I have a little update satement and I get an SQL error and can't seem to find what is the problem.

 

Here's the statement if anyone can help me out :

 

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

mysql_query(" UPDATE moving SET `firstname` = '{$_POST['firstname']}', `lastname` = '{$_POST['lastname']}', `email` = '{$_POST['email']}', `movedate` = '{$_POST['movedate']}', `adress1` = '{$_POST['adress1']}', `adress2` = '{$_POST['adress2']}', `app1` = '{$_POST['app1']}', `app2` = '{$_POST['app2']}', `city1` = '{$_POST['city1']}', `city2` = '{$_POST['city2']}, `prov1` = '{$_POST['prov1']}', `prov2` = '{$_POST['prov2']}', `ind1` = '{$_POST['ind1']}', `phone1` = '{$_POST['phone1']}', `ind2` = '{$_POST['ind2']}', `phone2` = '{$_POST['phone2']}', `postal1` = '{$_POST['postal1']}', `postal11` = '{$_POST['postal11']}', `postal2` = '{$_POST['postal2']}', `postal22` = '{$_POST['postal22']}' WHERE `id` = '{$_POST['id']}'") or die(mysql_error());
echo "<center><font face=arial size=2>Update Sucessful</font></center>";

?>

 

 

Any help would be be greatly appreciated!

Link to comment
https://forums.phpfreaks.com/topic/149182-solved-update-statement-sql-error/
Share on other sites

here's de SQL error:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'prov1', `prov2` = 'prov2', `ind1` = '111', `phone1` = '1111111', `ind2` = '222',' at line 1

Hmm, i would do it differently actually...

 

<?
//// First, you create the variables
$get_id = {$_POST['id']} ;
$get_firstname = {$_POST['firstname']} ;
$get_lastname = {$_POST['lastname']} ;
/////AND SO ON...



//// Second, you update each one on its own
mysql_query(" UPDATE moving SET firstname = '$get_firstname' WHERE id = '$get_id' ") ;
mysql_query(" UPDATE moving SET lastname = '$get_lastname' WHERE id =  '$get_id' ") ;
/////AND SO ON...

?>

 

You see what i mean?

 

This will work i think, but note that i have a unique way of writing my codes, so, you can wait for another answer which can be easier than mine.

You see what i mean? Each one alone

 

And the point of that would be what exactly?

 

@patheticsam

 

The problem is likely the fact that you not sanitising your input. You need to escape certain chars that will otherwise make sql choke and also open security holes. take a look at mysql_real_escape_string.

Very inefficiently though.

 

You are right, I had a problem sometime ago, which was: A slow server

I accused my Scripts and started changing them to become more efficient

My server is now faster actually, so, i guess it was my scripts

 

Anyway, i am not a programmer, i am a Mechanical Engineer

I am only doing this because i really like having a website

You can update more than one field at once, you just have a brain-stretching (for me anyway) job of getting the quotes and the brackets all right.

 

Instead of

 

mysql_query(" UPDATE moving SET `firstname` = '{$_POST['firstname']}', `lastname` = '{$_POST['lastname']}', `email` = '{$_POST['email']}',

 

try this :

 

mysql_query(" UPDATE moving SET firstname = '$_POST[firstname]', lastname = '$_POST[lastname]', email = '$_POST[email]',   ..  etc ...    ");

 

I've removed the single quote marks from off the field names everywhere and also taken out the curly brackets.

 

No guarantees!  But type it exactly like that and see if it works !

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.