www.webdeveloper.com
Results 1 to 12 of 12

Thread: Php - mysql UPDATE help

  1. #1
    Join Date
    Jul 2012
    Posts
    57

    Php - mysql UPDATE help

    I've searched the web and tried many different methods, and still failing. Heres the situation:

    <form is up here>

    <?php
    if(isset($_POST['save1'])) {
    $title1 = $_POST['title1'];
    $title2 = $_POST['title2'];
    $title3 = $_POST['title3'];
    $content1 = $_POST['content1'];
    $content2 = $_POST['content2'];
    $content3 = $_POST['content3'];
    if(!get_magic_quotes_gpc()) {
    $title1 = addslashes($title1);
    $title2 = addslahses($title2);
    $title3 = addslashes($title3);
    $content1 = addslashes($content1);
    $content2 = addslashes($content2);
    $content3 = addslashes($content3);
    }
    $con = mysql_connect(input my info...);
    if (!$con){
    die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("db", $con);
    $result = mysql_query("UPDATE 'homepage' SET title='$title1' WHERE id='1'");
    $result = mysql_query("UPDATE 'homepage' SET title='$title2' WHERE id='2'");
    $result = mysql_query("UPDATE 'homepage' SET title='$title3' WHERE id='3'");
    $result = mysql_query("UPDATE 'homepage' SET content='$content1' WHERE id='1'");
    $result = mysql_query("UPDATE 'homepage' SET content='$content2' WHERE id='2'");
    $result = myslq_query("UPDATE 'homepage' SET content='$content3' WHERE id='3'");

    mysql_close($con);
    echo "Edit complete";
    }
    ?>


    what would be the issue? Or any better method of going about this? (building a content manager) I already have the homepage pulling preset data from database and works fine.. but this is annoying!!!

  2. #2
    Join Date
    Aug 2012
    Posts
    155
    Your problem is the quote marks around your table name. Either remove them or use back-ticks instead

    'homepage' - wrong
    homepage - correct
    `homepage` - correct

    You also don't need quote marks around integers so they can be removed.

  3. #3
    Join Date
    Jul 2012
    Posts
    57
    Hello,

    Thanks for the reply, however even with that fix it still seems like my entries are just pretending to exist. How ever I will say, this page is acting extremely weird... I link my style sheet with same pages and this page randomly puts a few elements wayyy out of place, and reviewing the css after an hour their is NOTHING wrong, and confusing the hell out of me... I think someone is screwing with me lolz.

    But since maybe this script is not the problem.. I basically have this set up:

    homepage echos the database content in corrent position (I put some present text in there), and this script I put on is avaliable from a protected admin page that will perform what you see from the inputs of a form.. Everything is working but the updating portion and random css bug.

    Edit: every since I added your update, now the missplaced content i was talking about earlier has disapeared.. or all the html below this script 0_0

  4. #4
    Join Date
    Aug 2012
    Posts
    155
    Add mysql_error to the end of your update queries or run the query (replacing the variables with actual values) in PHPMyAdmin and see if you get an error.

    You also need to secure your queries by using mysql_real_escape_string on your posted data as at the moment it is open to php injection.

    You can also remove the $result = from each query line unless you are going to use it later in your script.

    As for your CSS I have no idea as that was not your question.

  5. #5
    Join Date
    Jul 2012
    Posts
    57
    No errors..!!??

  6. #6
    Join Date
    Jul 2012
    Posts
    57
    Ok, I added my own error report (via echoing at certain points). I found I stop getting echos at the start of the if(!get_magic_quotes_gpc()) { etc...

    any ideas?

  7. #7
    Join Date
    Aug 2012
    Posts
    155
    How did you test for errors? Using mysql_error or in PHPMyAdmin?

    By the way, you are repeating your queries unnecessarily as well:

    PHP Code:
    $result mysql_query("UPDATE 'homepage' SET title='$title1' WHERE id='1'");
    // AND
    $result mysql_query("UPDATE 'homepage' SET content='$content1' WHERE id='1'");
    // Should be combined (and I have re-formatted to how I would do it)
    mysql_query("UPDATE homepage SET title='".$title1."', content='".$content1."' WHERE id=1"); 

  8. #8
    Join Date
    Jul 2012
    Posts
    57
    Ok.. lol figured out the issue on query #6.. i wrote myslq instead of mysql... wow... CLOSE THREAD QUICK!!! lolz

  9. #9
    Join Date
    Aug 2012
    Posts
    155
    Quote Originally Posted by jazzmasterkc View Post
    Ok, I added my own error report (via echoing at certain points). I found I stop getting echos at the start of the if(!get_magic_quotes_gpc()) { etc...

    any ideas?
    Get rid of it, not really required nowadays.

    How I would format your PHP
    PHP Code:
    <?php
    if(isset($_POST['save1'])) {

    $con mysql_connect(input my info...);
    if (!
    $con){
    die(
    'Could not connect: ' mysql_error());
    }
    mysql_select_db("db"$con);

    $title1 mysql_real_escape_string(trim($_POST['title1']));
    $title2 mysql_real_escape_string(trim($_POST['title2']))));
    $title3 mysql_real_escape_string(trim($_POST['title3']));
    $content1 mysql_real_escape_string(trim($_POST['content1']));
    $content2 mysql_real_escape_string(trim($_POST['content2']));
    $content3 mysql_real_escape_string(trim($_POST['content3']));

    mysql_query("UPDATE homepage SET title='".$title1."', content='".$content1."' WHERE id=1");
    mysql_query("UPDATE homepage SET title='".$title2."', content='".$content2."' WHERE id=2");
    mysql_query("UPDATE homepage SET title='".$title3."', content='".$content3."' WHERE id=3");

    mysql_close($con);
    echo 
    "Edit complete";
    }
    ?>

  10. #10
    Join Date
    Jul 2012
    Posts
    57
    I echoed after each event.. like echo "connected to database! "; ..etc. I find this more productive as I can pin point things easier, then delete when Its 100%

    Ok thanks for that, I knew it existed but was trying to get the easier way to work first

  11. #11
    Join Date
    Jul 2012
    Posts
    57
    agh! Now I has another issue, everything is updating blank in the database.. link the variables got messed up or something.. let me see..

  12. #12
    Join Date
    Jul 2012
    Posts
    57
    Ah, interesting. After echoing the $title1 during each step, mysql_real_escape_string will make it to nothing.. or blank.. any idea as to why?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles