Jump to content

Recommended Posts

I am developing a news system for a website. I am about to finish it off, with code to edit the news. I have the following code.

 

$tester = "blah blah blah";
echo $tester ."<br>" .$title ."<br>";

$sql = "update tblnews set title=\"$title\" where count=" .$n2e;

 

The news title and body is loaded into a form for editing. $title is coming in POST data from the submit page.

 

$n2e is the unique code for each piece of news

 

The echo statement returns the values exactly as I want them to be:

 

blah blah blah

news title

 

However, when I comment out the echo code, the script seems to enter a blank title, even though the variable name from the echo code is exactly the same as that inside the query. I am not yet working on the code for the body text.

 

If I change the query to enter $tester, then it enters "blah blah blah" perfectly

 

I am so confused and any help would be great!

 

Stephen

Link to comment
https://forums.phpfreaks.com/topic/2551-mysql-annoyance/
Share on other sites

have you tried to use mysql_error() to debug the query? I think your server has magic quote turned on, which will add backslashes to any quote marks present in the submitted text. try stripslashes() before putting the results into the query.

297896[/snapback]

 

 

Hi,

 

There doesnt seem to be any error coming from the mysql query. I have this to test everything went OK:

 

$go = mysql_query($sql, $conn) or die ("could not execute query to update");

if($go) {echo "news article edited successfully";}

 

... and there are no slashes in the submitted test .. this is just simple "test2" stuff at the minute :)

 

Thanks for your reply but Im afraid it's still the same!

Stephen

Link to comment
https://forums.phpfreaks.com/topic/2551-mysql-annoyance/#findComment-8471
Share on other sites

EDITED *NVM* stupid question

297907[/snapback]

 

Anyone have a solution to this? I have come accross another project and have the exact same problem. I am able to echo the query, and put it into phpmyadmin, but yet when I try to run it through PHP it just inserts blank values

 

Stephen

Link to comment
https://forums.phpfreaks.com/topic/2551-mysql-annoyance/#findComment-8512
Share on other sites

Single qoutes returns an error. The current code in my page is as follows:

 

<?php

$id = $_GET['id'];
$score1 = $_POST['score1'];
$score2 = $_POST['score2'];


include "sqlconn.php";

$sql = "INSERT INTO tbldiv1 (ID, score1, score2) VALUES ($id, '$score1', '$score2') ON DUPLICATE KEY UPDATE score1 = '$score1', score2 = '$score2'";

$sql = $sql;

$go = mysql_query($sql, $conn) or die ("could not execute query 1 to update");

if($go) {echo "Result recorded";}



?>

 

Using the UPDATE idea does the same thing - everything seems fine but the result entered is blank .. eventhough when I echo the query and put it into PHPMyAdmin its all good.

So simple but it just wont work!...

 

Stephen

Link to comment
https://forums.phpfreaks.com/topic/2551-mysql-annoyance/#findComment-8526
Share on other sites

Instead of:

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--][span style=\"color:#0000BB\"]<?php

$sql [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]$sql[/span][span style=\"color:#007700\"];

 

[/span][span style=\"color:#0000BB\"]$go [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$sql[/span][span style=\"color:#007700\"], [/span][span style=\"color:#0000BB\"]$conn[/span][span style=\"color:#007700\"]) or die ([/span][span style=\"color:#DD0000\"]\"could not execute query 1 to update\"[/span][span style=\"color:#007700\"]);

 

if([/span][span style=\"color:#0000BB\"]$go[/span][span style=\"color:#007700\"]) {echo [/span][span style=\"color:#DD0000\"]\"Result recorded\"[/span][span style=\"color:#007700\"];}

[/span][span style=\"color:#0000BB\"]?>[/span]

[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

try

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--][span style=\"color:#0000BB\"]<?php

[/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#DD0000\"]\'Will now attempt to perform <span style=\"font-weight:bold\">\' [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#0000BB\"]$sql [/span][span style=\"color:#007700\"]. [/span][span style=\"color:#DD0000\"]\'</span><br>\'[/span][span style=\"color:#007700\"];

 

[/span][span style=\"color:#0000BB\"]$go [/span][span style=\"color:#007700\"]= [/span][span style=\"color:#0000BB\"]mysql_query[/span][span style=\"color:#007700\"]([/span][span style=\"color:#0000BB\"]$sql[/span][span style=\"color:#007700\"], [/span][span style=\"color:#0000BB\"]$conn[/span][span style=\"color:#007700\"]) or die ([/span][span style=\"color:#DD0000\"]\"could not execute query 1 to update\"[/span][span style=\"color:#007700\"]);

 

if([/span][span style=\"color:#0000BB\"]mysql_affected_rows[/span][span style=\"color:#007700\"]() > [/span][span style=\"color:#0000BB\"]0[/span][span style=\"color:#007700\"]) echo [/span][span style=\"color:#DD0000\"]\"Result recorded\"[/span][span style=\"color:#007700\"];

else echo [/span][span style=\"color:#DD0000\"]\'There was a problem with the query\'[/span][span style=\"color:#007700\"];

[/span][span style=\"color:#0000BB\"]?>[/span]

[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

Or you could do

[!--PHP-Head--][div class=\'phptop\']PHP[/div][div class=\'phpmain\'][!--PHP-EHead--][span style=\"color:#0000BB\"]<?php

[/span][span style=\"color:#007700\"]echo [/span][span style=\"color:#0000BB\"]mysql_info[/span][span style=\"color:#007700\"]();

[/span][span style=\"color:#0000BB\"]?>[/span]

[/span][!--PHP-Foot--][/div][!--PHP-EFoot--]

instead of the if at the end.

 

See if you get any more relavent information from these statements.

 

Ken

 

Link to comment
https://forums.phpfreaks.com/topic/2551-mysql-annoyance/#findComment-8532
Share on other sites

what version of MySQL are you running on? The ON DUPLICATE KEY syntax is not available until 4.1.0

 

the same thing worked in phpMyAdmin? Are you sure you're copying the query from the view source, not the browser output? You know browsers parse out the tags so you get clean text when you copy.

Link to comment
https://forums.phpfreaks.com/topic/2551-mysql-annoyance/#findComment-8533
Share on other sites

Hi,

 

Thaks for your feedback.

 

@kenrbnsn:

 

I tried your debugging code but it returned all true - eventhough nothing was actually inserted it returned "Result recorded".

 

@ryanlwh:

 

I have mySQL 4.1.10. I also did try the UPDATE tbldiv1 SET ... and the output was the same.

I did try copying from source but that also worked perfectly from inside PHPMyAdmin.

 

----

 

When I take the query from the view:source and put it into the script, then everything is perfect - could the variables lose their values somehow while the query is going on - and is there any way I could test for this?

 

Thanks for your continued suggestions and help!

Stephen

Link to comment
https://forums.phpfreaks.com/topic/2551-mysql-annoyance/#findComment-8537
Share on other sites

Just for grins try this:

 

$sql = "INSERT INTO tbldiv1 (ID, score1, score2) VALUES (99, '10', '11') ON DUPLICATE KEY UPDATE score1 = '12', score2 = '13'";

 

This will tell you whether the problem is your variables or not.

 

Also your update should have added "WHERE ID = '$id'" to it. (I *think*)

Link to comment
https://forums.phpfreaks.com/topic/2551-mysql-annoyance/#findComment-8576
Share on other sites

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.