Reply to topic  [ 9 posts ] 
More PHP help 
Author Message
Spends far too much time on here
User avatar

Joined: Fri Apr 24, 2009 8:38 am
Posts: 2967
Location: Dorchester, Dorset
Reply with quote
I'm trying to update some records in my database. I'm sure I've made some sort of error with my syntax in the '$sql=' line. Does anybody know where I'm going wrong? I've not quite got the hang of when to use single quotes, double quotes or brackets!

Code:
<?php
   $mysqli = mysqli_connect("localhost", "root", "root", "Job_list");
   
   $sql = "UPDATE Jobs SET Description = '$_POST["description"]', Status = '$_POST["status"]', Notes = '$_POST["notes"]' WHERE Job_number = '$_POST[jobnumber]'";
   
   $res = mysqli_query($mysqli, $sql);   
   
   if ($res === TRUE) {
         header('Location: ../joblist.php');

   } else {
      printf("Could not update record: %s\n", mysqli_error($mysqli));
   }

   mysqli_close($mysqli);

?>

_________________
I've finally invented something that works!

A Mac User.


Mon May 10, 2010 3:32 pm
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 11:36 pm
Posts: 3527
Location: Portsmouth
Reply with quote
In the $sql line you have a lot of " quote marks.

You need one pair, to signal the start and end of the string to be stored within the $sql variable. Any others need to be escaped with a back-slash, so PHP knows that you don't mean it's the end of the line.

So in your code you will need to escape as such:

Code:
$sql = "UPDATE Jobs SET Description = '$_POST[\"description\"]', Status = '$_POST[\"status\"]', Notes = '$_POST[\"notes\"]' WHERE Job_number = '$_POST[\"jobnumber\"]' ";


Also - it seems you are pulling the data straight from the user input. This can be dangerous - are you checking it first?

I don't tend to do it this way - I've got a habit of pulling the data out of the post array, and processing with addslashes() etc as it goes into another.

Infact, I'm not even sure the above code will work. It might add the literal string $_POST["description"] etc rather than the data held in the $_POST[] array. If you find that is the case, you'll need to pull the data out of the $_POST[] array and into a new one as mentioned. It's been a long time since I've done any PHP, so I can't remember exactly how it's likely to behave in this instance.

_________________
Image


Mon May 10, 2010 3:40 pm
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 11:36 pm
Posts: 3527
Location: Portsmouth
Reply with quote
P.S. Take a look at these functions for preparing your data for the DB query:

mysql_real_escape_string

addslashes

_________________
Image


Mon May 10, 2010 3:50 pm
Profile
Spends far too much time on here
User avatar

Joined: Fri Apr 24, 2009 8:38 am
Posts: 2967
Location: Dorchester, Dorset
Reply with quote
Cheers Nick, I shall absorb and digest. I'm going straight from user input as it's an internal database and doesn't need checking.

_________________
I've finally invented something that works!

A Mac User.


Mon May 10, 2010 3:58 pm
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 11:36 pm
Posts: 3527
Location: Portsmouth
Reply with quote
Cool. In that case, there is another option:

Code:
$sql = "UPDATE Jobs SET Description = '". $_POST["description"] . "', Status = '" . $_POST["status"] . "', Notes = '" . $_POST["notes"] ."' WHERE Job_number = '" . $_POST["jobnumber"] . "'";


It's horrendously ugly, but will get the job done. :P

_________________
Image


Mon May 10, 2010 4:03 pm
Profile
Spends far too much time on here
User avatar

Joined: Fri Apr 24, 2009 8:38 am
Posts: 2967
Location: Dorchester, Dorset
Reply with quote
I'd love to be able to have a look at what you suggested, but I decided to look at something else and got stuck again! I've got a drop down list that populates based on the contents of a column. I want the drop down to be pre-selected with the current selection. I've looked all over the web and the code seems to be correct, but it sets the option to "selected" on every single option, not just the one that matches the current selection.

Code:
      <td><select name="status">

      <?php      
      }
      ?>
      
         <?php
         //Script to retrieve statuses and populate drop down
         $mysqli = mysqli_connect("localhost", "root", "root", "Job_list");
         $sql = "SELECT Status FROM Statuses";
         $res = mysqli_query($mysqli, $sql);
   
         while ($line = mysqli_fetch_array ($res, MYSQLI_ASSOC)) {
         ?>
         <option value="<?php echo $line[Status];?>"<?php if ($line[Status] === $_POST[status]); {echo 'selected="selected"';}?>><?php echo $line[Status];?></option>      
         <?php      
         }
         ?>
         </select></td>


It seems that it thinks the value of $line[Status] is always the same as the $_POST[status] which it's definitely not…

_________________
I've finally invented something that works!

A Mac User.


Mon May 10, 2010 9:52 pm
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 11:36 pm
Posts: 3527
Location: Portsmouth
Reply with quote
There appears to be a couple of syntax errors.

First, there is a semi-colon after the if statement, which shouldn't be there. I don't know what effect it's likely to have, but it's likely to make PHP think that is the end of the if statement, IE there is no conditional statement to follow.

Also, the echo statement has some errors. Try this code:

Code:
<?php
if ($line[Status] == $_POST[status])
echo "selected=\"selected\" ";
?>


What options are you running in your .ini file? I'm guessing that you have not set errors to be printed - it would make things much easier if you were to enable these. That way, if PHP runs into a problem, like the syntax errors in the code above, it can print out a helpful message saying what error it had and even what line of code caused it.

If you are unable to change the php.ini configuration file, you can change the configuration at run time using the ini_set() method as detailed [url]here[/url]. That page includes some code you can include in your scripts, so that if there is an error it will give you some output hinting at the problem.

_________________
Image


Mon May 10, 2010 11:22 pm
Profile
Spends far too much time on here
User avatar

Joined: Fri Apr 24, 2009 8:38 am
Posts: 2967
Location: Dorchester, Dorset
Reply with quote
Cheers Nick, you're bang on, it was the semi colon. That must have been the only thing I overlooked and I was tearing my hair out for hours! Now to go back to the data update…

_________________
I've finally invented something that works!

A Mac User.


Tue May 11, 2010 8:29 am
Profile
Spends far too much time on here
User avatar

Joined: Fri Apr 24, 2009 8:38 am
Posts: 2967
Location: Dorchester, Dorset
Reply with quote
You were also right about the quotes, that was the problem on the update part. I thought I had tried absolutely every combination of everything, but obviously not! Thanks very much for your help.

_________________
I've finally invented something that works!

A Mac User.


Tue May 11, 2010 9:00 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 9 posts ] 

Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.