1

I have some code that I am trying to make into a news feed for my front page. So far I cant get the Update to work. Ps feel free to clean up code and make for practical as this is version 1 I haven't cleaned code up. Thanks Joshua

Update Part

    if(isset($_POST['edit']))
    {
    $checkbox = $_POST['checkbox'];

    for($i=0;$i<count($_POST['checkbox']);$i++){

    $edit_id = $checkbox[$i];

        echo "<textarea name='editsavetext' id='editsavetext' rows='4' cols='50'>$edit_id</textarea>";
    }};

    if(isset($_POST['editsave'])){

    echo $editsavetext;

    $editsavetext = $_POST['editsavetext'];

    $sql='UPDATE $tbl_name SET home_show = $editsavetext WHERE home_show = $edit_id';

    $result = mysql_query($sql);

    if($result){
echo "<meta http-equiv='refresh' content='0; url=http://****' />";}};

For Rich Bradshaw

Well at the moment the code doesnt work because i need the system to remember what checkbox was ticks and what is being edited using $edit_id because it doesnt remember the results when being saved so it doesnt effect anything as such.

if(isset($_POST['edit']))
{
$checkbox = $_POST['checkbox'];

for($i=0;$i<count($_POST['checkbox']);$i++){

$edit_id = $checkbox[$i];

    echo "<textarea name='editsavetext' id='editsavetext' rows='4' cols='50'>$edit_id</textarea>";
}};

if(isset($_POST['editsave'])){

echo $editsavetext;

$editsavetext = $_POST['editsavetext'];

$sql="UPDATE `$tbl_name` SET home_show = '$editsavetext' WHERE home_show=':edit_id'";

$result = mysql_query($sql);

if($result){echo "<meta http-equiv='refresh' content='0; url=http://****' />";}};
2
  • If you change your query to $sql="UPDATE $tbl_name SET home_show = '$editsavetext' WHERE home_show = '$edit_id'";, will it work? Commented Oct 1, 2014 at 9:03
  • Note: the mysql_* functions are deprecated and will be removed from future versions of PHP. You should not write new code using them. Use mysqli_* or PDO instead. Commented Oct 1, 2014 at 9:04

2 Answers 2

1

Without digging through all of that,

'UPDATE $tbl_name SET home_show = $editsavetext WHERE home_show = $edit_id'

should be

"UPDATE `$tbl_name` SET home_show = '$editsavetext' WHERE home_show = $edit_id"

if you had code like:

$a = "hi";

echo 'I say $a';

it would write out I say $a.

if you had

echo "I say $a";

it would write I say hi.

Also, text needs to be in speechmarks in SQL queries.

Then you need to look up SQL Injection, as this code is not safe to use without protecting against it. Finally, why are you using mysql_query? Read the docs on it: http://php.net/manual/en/function.mysql-query.php - note that it's deprecated.

enter image description here

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

11 Comments

Oops.. home_show='$edit_id'. I understand, you eagerly wants to post your answer. ;)
I wouldn't bother doing that as it's an integer.
At this point you need to check through the rest of the code and make sure you haven't made the same/similar mistakes elsewhere. Reduce your code to just the sql part, does that work? If so, this problem is solved, but you have more problems.
I worked out the problem is inserting the output $edit_id into it but i havn't fixed it yet
I'd assumed that $edit_id was an integer, if not, then wrap that in speechmarks too.
|
0

Your update statement seems to be wrong.
You are using single quotes and using $edit_id inside it. Use double quotes for that.
Try this (Assuming that home_show are ints.)-

$sql="UPDATE $tbl_name SET home_show = $editsavetext WHERE home_show = $edit_id";
     ^                                                                         ^

Assuming home_show column is varchar

$sql="UPDATE $tbl_name SET home_show = '$editsavetext' WHERE home_show = '$edit_id'";
     ^                                 ^             ^                   ^        ^^

Comments

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.