0

I'm new to php and think this should work but I can't figure out if I'm doing something wrong with syntax or what. This is the script that handles my submitted data from a form.

I set php variables to the posted data from the form, it echos out the correct data in the variables, the problem is with updating the record in the database, it doesn't...

The table in the DB has 3 attributes, adID, iconURL, webURL.

The form has 2 input textfields, and a hidden field, iconPath, webPath, and recordN.

<?php
$recNum = $_POST["recordN"];
$iconU = $_POST["iconPath"];
$webU = $_POST["webPath"]; 
echo 'Number of record updated: ' . $recNum;
echo '<br />New Icon Path: ' . $iconU;
echo '<br />New Web Path: ' . $webU;

$con = mysql_connect("localhost","admin","pass");
if (!$con)  {   die('Could not connect: ' . mysql_error());   }

mysql_select_db("DBNAME", $con);

mysql_query("UPDATE adSources set iconURL = $iconU, webURL = $webU 
WHERE adID = $recNum");

mysql_close($con);

echo '<br /><a href="http://mydomain.com/thePage.html" target="_blank">Return to main page</a>' . "\n"; 
?> 

So where adID = recNum, I want to overwrite iconURL = $iconU and webURL = $webU

I have the value in $iconU and $webU that I want to use, and iconURL and webURL are the names of the fields in the database. I read them just fine and can display them by those names just fine, I just can't update this.

I granted full permissions in the db for the user and all that.

Any help? Php seems so freakin ugly compared to other languages.

2 Answers 2

1

You missed out quotes along your variable names. Try:

mysql_query("UPDATE adSources set iconURL = '$iconU', webURL = '$webU'
            WHERE adID = $recNum");

Better if you could also escape the variables:

mysql_query("UPDATE adSources set iconURL = '".mysql_real_escape_string($iconU).
            "', webURL = '".mysql_real_escape_string($webU).
            "' WHERE adID = $recNum");

That makes it a little better, but the use of mysql_real_escape_string() is actually discouraged. You should be checking out and replacing it with PDO once you get the hang of it.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much, I've spent all afternoon trying various combinations of quotes, ditching the variables for '$_POST[field]', etc. Your answer works perfectly. I've always avoided php because it looks so aweful: periods, underscores, ->, etc. in javascript and C# things look beautiful. But for what it does this seems pretty amazing and I guess I need to memorize the differences in the language. Thank you so much it works perfectly
1

To debug, use mysql_error(). You'll have an error in your syntax as you need to wrap the strings ($iconU etc) in quotes.

BUT... you'll be much better off learning PDO; this will handle the wrapping and making safe of variables for you. mysql functions are being depreciated, so don't start with the old stuff, start with the new! http://php.net/manual/en/book.pdo.php

$sth = $dbh->prepare('UPDATE adSources set iconURL = :iconU, webURL = :webU WHERE adID = :recNum');
$sth->bindValue(':iconU', $iconU , PDO::PARAM_STR);
$sth->bindValue(':webU', $webU , PDO::PARAM_STR);
$sth->bindValue(':recNum', $recNum , PDO::PARAM_INT);
$sth->execute();

Edit: responding to your comment about "freaking ugly". Not intending to start a debate, but yes, it starts ugly. Then you wrap this up into a nice class, get your error handling right (wrap in try/ctach) and you're laughing. So Start with the ugly and you'll soon learn how to clean it up.

1 Comment

Thanks I'll get started on that, never heard of PDO before. I just had to take a sql class for school so am familiar with sql language, normalization, etc but never heard of PDO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.