Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post

    simple syntax error

    PHP Code:
    mysql_query("update $tbl_name set firstname=$fname, lastname=$lname where firstname=$firstname and lastname=$lastname"
    or die(
    mysql_error());
    echo 
    "Successful!"
    Is returning:
    syntax error near 'and lastname=' at line 1

  • #2
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    are my variables supposed to be in some sort of quotes?

  • #3
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Strings should be single-quoted. Integers don't have to be.

  • #4
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    why isn't this actually updating my information? Everything should be correct, but nothing is happening.

    PHP Code:
    <?php
    mysql_connect
    ("$host""$dbusername""$dbpassword")or die(mysql_error());
    mysql_select_db("$dbname")or die(mysql_error());
    $step $_GET['step'];
    $firstname $_POST['firstname'];
    $lastname $_POST['lastname'];
    $fname $_POST['fname'];
    $lname $_POST['lname'];
    if (
    $step == selection) {
    if (
    $fname == "" || $lname == "") {
    echo 
    "No fields can be left empty.";
    exit;
    }
    mysql_query("update $tbl_name set firstname='$fname', lastname='$lname' where firstname='$firstname' and lastname='$lastname'"
    or die(
    mysql_error());
    echo 
    "Successful!";
    exit;
    }
    if (
    $step == retrieve) {
    $select mysql_fetch_array(mysql_query("select * from $tbl_name where firstname='$firstname' && lastname='$lastname'"));
    if (!
    $select) {
    echo 
    "Not a good name.";
    exit;
    }
    echo 
    "Hello <b>$firstname $lastname</b>!<br>";
    echo 
    "<form method=post action=?step=selection>";
    echo 
    "First name: <input type=\"text\" name=\"fname\" value=\"$select[firstname]\" /><br>";
    echo 
    "Last name: <input type=\"text\" name=\"lname\" value=\"$select[lastname]\" /><br>";
    echo 
    "<input type=\"submit\" value=\"Submit\" /></form>";
    exit;
    }
    ?>
    <form method="post" action="?step=retrieve">
    First name:<input type="text" name="firstname" /><br>
    Last Name: <input type="text" name="lastname" /><br>
    <input type="submit" value="Submit" /></form>
    It says "Successfull" but nothing is actually changed.

  • #5
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    When using variables in queries, I prefer to set the query into a separate variable so it's easier to echo and see exactly what you're sending to the db:

    Code:
    $query = "update $tbl_name set firstname='$fname', lastname='$lname' where firstname='$firstname' and lastname='$lastname'";
    mysql_query($query) or die(mysql_error());
    Now you can echo $query and see what you're variables are being set as, and that can help with your debugging.

  • #6
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    ok, the query is just this:

    update bsa_scouts set firstname='---', lastname='-----' where firstname='' and lastname=''
    i know that it just isn't getting any info for the firstname and lastname original so it doesn't know where to edit, but im not sure how to fix my code to work.

    PHP Code:
    <? include '../includes/config.inc.php'?>
    <? 
    include '../includes/func.inc.php'?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link rel="stylesheet" type="text/css" href="../stylesheet.css"/>
            <title>Edit Information</title>
    </head>

    <body>
    <div class="wrapper">
                <div class="title">
                    <img class="title_image" src="../flourdeleis_wood.png" alt="Header Image" width="180px" height="150px"/>
                    Edit Information        
                </div>
    <? include 'includes/memnav.inc.php';?>
    <div class="main">
    <?php
    mysql_connect
    ("$host""$dbusername""$dbpassword")or die(mysql_error());
    mysql_select_db("$dbname")or die(mysql_error());
    $step $_GET['step'];
    $firstname $_POST['firstname'];
    $lastname $_POST['lastname'];
    $fname $_POST['fname'];
    $lname $_POST['lname'];
    if (
    $step == selection) {
    if (
    $fname == "" || $lname == "") {
    echo 
    "No fields can be left empty.";
    exit;
    }
    $updatequery "update $tbl_name set firstname='$fname', lastname='$lname' where firstname='$firstname' and lastname='$lastname'";
    mysql_query($updatequery
    or die(
    mysql_error());
    echo 
    "Successful!".$updatequery;
    exit;
    }
    if (
    $step == retrieve) {
    $select mysql_fetch_array(mysql_query("select * from $tbl_name where firstname='$firstname' && lastname='$lastname'"));
    if (!
    $select) {
    echo 
    "Not a good name.";
    exit;
    }
    echo 
    "Hello <b>$firstname $lastname</b>!<br>";
    echo 
    "<form method=post action=?step=selection>";
    echo 
    "First name: <input type=\"text\" name=\"fname\" value=\"$select[firstname]\" /><br>";
    echo 
    "Last name: <input type=\"text\" name=\"lname\" value=\"$select[lastname]\" /><br>";
    echo 
    "<input type=\"submit\" value=\"Submit\" /></form>";
    exit;
    }
    ?>
    <form method="post" action="?step=retrieve">
    First name:<input type="text" name="firstname" /><br>
    Last Name: <input type="text" name="lastname" /><br>
    <input type="submit" value="Submit" /></form>                
                </div>
                <div class="copyright">
                    <?=$copyright;?>    
                </div>
            </div>

    </body>
    </html>

  • #7
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You're looking for the elements 'fname' and 'lname' in your POST array, but your form doesn't have any 'fname' or 'lname' fields -- you'll need to add them.

  • #8
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    it does have fname and lname.

    There are two forms, this is the second one:

    PHP Code:
    echo "Hello <b>$firstname $lastname</b>!<br>";
    echo 
    "<form method=post action=?step=selection>";
    echo 
    "First name: <input type=\"text\" name=\"fname\" value=\"$select[firstname]\" /><br>";
    echo 
    "Last name: <input type=\"text\" name=\"lname\" value=\"$select[lastname]\" /><br>";
    echo 
    "<input type=\"submit\" value=\"Submit\" /></form>";
    exit; 

  • #9
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    My mistake -- you're missing $firstname and $lastname.

    The first form defines them and brings up the second form. But when you submit the second form, you lose the values for $firstname and $lastname. POST data does not carry over from multiple form submissions. You need to either use sessions to keep those values or place them in your second form as hidden fields. The second option is easier and will probably work fine for you.

  • #10
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    ...okay...so how do i do that...?

    lol

  • #11
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts

  • #12
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    doing hidden fields like that however, means that someone can download the form, change the html, and reupload it to wherever they are, and run it. I want it to be php.

  • #13
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Then use a session. I think we've ventured pretty far outside MySQL, though, so any session questions should be raised in the PHP forum.

  • #14
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    i pmed a moderator and hopefully this will just be moved.

    Can you explain how to add sessions to my code? I know what a session generally is, but im not completely sure how to use them.

  • #15
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    *bump*


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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