MYSQL help

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by xcaliberse, Dec 17, 2008.

  1. xcaliberse

    xcaliberse New Member

    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
    Hey, so i created a mysql server, and i jus wanted to make a form that does feedback. so i have this
    <script>
    <?
    $firstname=$_POST['Name'];
    $message=$_POST['Message'];


    $submit=$_POST['submit'];
    if($submit=="Submit"){


    $con = mysql_connect("heres the problem","username,"password");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("learning_beta", $con);

    mysql_query("INSERT INTO Persons (Name, Message)
    VALUES ('$firstname', '$lastname')");

    echo "DONE";
    }

    ?>

    <form method="post">
    Firstname: <input type="text" name="Name"><br />
    Message: <input type="text" name="Message"> <br />
    <input type="submit" name="submit" value="Submit">
    </form>
    </script>

    The problem is, how do i know what the login is? i have hte user name and password but iono the site for the phpMyAdmin login or whatever its suppsoed to be.
     
  2. tittat

    tittat Active Member

    Messages:
    2,478
    Likes Received:
    1
    Trophy Points:
    38
    ******Thread Moved to programming Help******
     
  3. xcaliberse

    xcaliberse New Member

    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
  4. vol7ron

    vol7ron New Member

    Messages:
    434
    Likes Received:
    0
    Trophy Points:
    0
    you bumped a thread after being moved 3 mins earlier? haha


    it's going to be 'localhost' or possibly 'localhost:5432'


    don't forget that your db and username have your x10name in front of it:
    xcaliberse_dbname
    xcaliberse_username
    mySQLpw
     
    Last edited: Dec 17, 2008
  5. xcaliberse

    xcaliberse New Member

    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
    I dont think "localhost:5432" or "localhost" is the appropriate thing to use for the mysql hosting link. The link should lead u directly to the phpMyAdmin login page, but any link i try just gets me directly to the control panel login.
     
  6. infurTH

    infurTH New Member

    Messages:
    44
    Likes Received:
    0
    Trophy Points:
    0
    it is bump and it is localhost
     
  7. xcaliberse

    xcaliberse New Member

    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
    So for

    $con = mysql_connect("heres the problem","username,"password");

    I put "localhost" for "heres the problem"? or no?
     
  8. vol7ron

    vol7ron New Member

    Messages:
    434
    Likes Received:
    0
    Trophy Points:
    0
    If you're trying to access MySQL through your code, then yes. <you could have tested it by now>

    If you're trying to get to phpMyAdmin, then no. But you're not going to be managing MySQL through phpMyAdmin in your code. You cannot connect to phpMyAdmin using mysql_connect() ~ that's absurd, MySQL is a database, phpMyAdmin is an application to interface the database.

    Just use localhost and you will see.
     
    Last edited: Dec 17, 2008
  9. xcaliberse

    xcaliberse New Member

    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
    Ok heres my new code

    <?
    $firstname=$_POST['Name'];
    $message=$_POST['Message'];


    $submit=$_POST['submit'];
    if($submit=="submit"){


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

    mysql_select_db("learning_beta", $con);

    mysql_query("INSERT INTO Persons (Firstname, Message)
    VALUES ('$firstname', '$message')");

    echo "DONE";
    }

    ?>

    <form method="post">
    Firstname: <input type="text" name="Name"><br />
    Message: <input type="text" name="Message"> <br />
    <input type="submit" name="submit" value="Submit">
    </form>

    Okay even if i type in jdkas;lfe instead of localhost, it doesnt give me the die(...); error. And it doesnt save it into the table that i made using phpMyAdmin. Can someone help me? I even manually added something via phpMyAdmin to the table and it doesnt show on the site. its www.kevinliang.x10hosting.com/feedback.php thanks.
     
  10. crisp

    crisp New Member

    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    In you're script
    Code:
    if($submit=="submit"){
    
    is always going to evaluate to false, because, in your form, the value for submit is "Submit" not "submit". Thus, the db connect and subsequent insert isn't ever going to happen.

    Try replacing that line with this instead
    Code:
    if ($submit=="Submit") {
    
    or even
    Code:
    if (strtolower($submit)=="submit") {
    
    and as previously stated by others, localhost is what you should be using for the host name to connect to the db ;)
     
  11. leafypiggy

    leafypiggy Manager of Pens and Office Supplies Staff Member

    Messages:
    3,819
    Likes Received:
    163
    Trophy Points:
    63
    Its always a good idea to have an else statement after an if.

    That way you can trouble shoot it.

    You also might want to secure your connection to the database as to stop sql injections.
     
  12. vol7ron

    vol7ron New Member

    Messages:
    434
    Likes Received:
    0
    Trophy Points:
    0
    Good eye crisp! I generally only use small variable names, so I'm not sure if parameters are case-sensitive (most likely are), but if that's the case, this is the culprit.

    xcali, it's best to build programs like these in stages (e.g., first having nothing but the connection, so you know the problem is not with that, then build the if statement or conditionals around it)
    It's also good to have have some sort of debugging feature embedded into it (like having an alert() every 5 lines of code in JavaScript), but remove all the debugging stuff from the production version.

    If you never get inside the IF-statement, then it'll never try to connect.
     
    Last edited: Dec 18, 2008
  13. brunoais

    brunoais New Member

    Messages:
    115
    Likes Received:
    0
    Trophy Points:
    0
    why don't you shorten:
    PHP:
    $con mysql_connect("localhost","username","password");
    if (!
    $con)
    {
    die(
    'Could not connect: ' mysql_error());
    }
    to:
    PHP:
    $con mysql_connect("localhost","username","password") OR die("Could not connect: "mysql_error());
    ?
     
  14. vol7ron

    vol7ron New Member

    Messages:
    434
    Likes Received:
    0
    Trophy Points:
    0
    I know you can (and should) do that in Perl, is that the correct syntax for PHP?
     
  15. xcaliberse

    xcaliberse New Member

    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
    Okay so heres my script now

    <?
    $firstname=$_POST['Name'];
    $message=$_POST['Message'];

    $submit=$_POST['submit'];
    if($submit=="Submit"){

    $con = mysql_connect("localhost","username","password");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("Feedback", $con);
    mysql_query("INSERT INTO Feedback (Firstname, Message)
    VALUES ('$Name, '$Message')");
    echo "DONE";
    }
    echo "$firstname";
    echo "$message";
    ?>
    <form method="post">
    Firstname: <input type="text" name="Name"><br />
    Message: <input type="text" name="Message"> <br />
    <input type="submit" name="submit" value="Submit">
    </form>

    and here is the pic of the database
    http://i253.photobucket.com/albums/hh74/xcaliberse/blah.jpg


    The problem is, when I submit it, it doesnt add it to the table.
     
  16. crisp

    crisp New Member

    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    The vars you're getting from post vars
    Code:
    $firstname=$_POST['Name'];
    $message=$_POST['Message'];
    
    doesn't match the values you're inserting in the table
    Code:
    mysql_query("INSERT INTO Feedback (Firstname, Message)
    VALUES ('$Name, '$Message')");
    
    change the above insert to match the vars you set earlier like so
    Code:
    mysql_query("INSERT INTO Feedback (Firstname, Message)
    VALUES ('$firstname, '$message')");
    
     
  17. xcaliberse

    xcaliberse New Member

    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
    Anyways I fixed what you said and it STILL wont save to the table.
    Code:
    <?
    $firstname=$_POST['Name'];
    $message=$_POST['Message'];
    
    
    $submit=$_POST['submit'];
    if($submit=="Submit"){
    
    
    $con = mysql_connect("localhost","username","password");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("Feedback", $con);
    
    mysql_query("INSERT INTO Feedback (Name, Message) 
    VALUES ('$firstname', '$message')");
    
    echo "DONE";
    }
    echo "$firstname";
    echo "$message";
    ?>
    
    <form method="post">
    Firstname: <input type="text" name="Name"><br />
    Message: <input type="text" name="Message"> <br />
    <input type="submit" name="submit" value="Submit">
    </form>
    
    http://i253.photobucket.com/albums/h...berse/blah.jpg
    ^try checking this image maybe it might help?
     
  18. blobtech

    blobtech New Member Prime Account

    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    1
    It could be that the mysql_query or mysql_select_db is triggering an error which is not caught. Try this:

    PHP:
    <?
        
    $firstname $_POST['Name'];
        
    $message $_POST['Message'];
        
        
        
    $submit $_POST['submit'];
        if(
    $submit == "Submit")
        {
            
    $con mysql_connect("localhost","username","password");
            if (!
    $con)
            {
                die(
    'Could not connect: ' mysql_error());
            }

            
    $succes mysql_select_db("Feedback"$con);
            if(!
    $succes)
            {
                die(
    'Could not select database: ' mysql_error());
            }

            
    $succes mysql_query("INSERT INTO Feedback (Name, Message) VALUES ('$firstname', '$message')");
            if(!
    $succes)
            {
                die(
    'Could not execute SQL query: ' mysql_error());
            }

            echo 
    "DONE";
        }
        
        echo 
    "$firstname";
        echo 
    "$message";
    ?>

    <form method="post">
    Firstname: <input type="text" name="Name"><br />
    Message: <input type="text" name="Message"> <br />
    <input type="submit" name="submit" value="Submit">
    </form>
     
  19. brunoais

    brunoais New Member

    Messages:
    115
    Likes Received:
    0
    Trophy Points:
    0
    why not instead of:
    PHP:
    $submit=$_POST['submit'];
    if(
    $submit=="Submit"){
    you use this code?
    PHP:
    if($_POST['submit']=="Submit"){
    Makes the code simpler :)
    Edit:
    the simpler it is the less probable is for you to get an error ;)
     
    Last edited: Dec 20, 2008
  20. mrxp_anupam

    mrxp_anupam New Member

    Messages:
    65
    Likes Received:
    0
    Trophy Points:
    0
    True about the last quote, but the issue here is regarding the mysql connection.
    Please make sure that you have connected to localhost.
    Are you using a testing server or connecting to some online server.
    The second thing is that don't write:
    instead write:
    Thats enough.
     

Share This Page