MYSQL help

xcaliberse

New Member
Messages
46
Reaction score
0
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.
 

vol7ron

New Member
Messages
434
Reaction score
0
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:

xcaliberse

New Member
Messages
46
Reaction score
0
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.
 

xcaliberse

New Member
Messages
46
Reaction score
0
Points
0
So for

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

I put "localhost" for "heres the problem"? or no?
 

vol7ron

New Member
Messages
434
Reaction score
0
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:

xcaliberse

New Member
Messages
46
Reaction score
0
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.
 

crisp

New Member
Messages
85
Reaction score
0
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 ;)
 

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
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.
 

vol7ron

New Member
Messages
434
Reaction score
0
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".

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:

brunoais

New Member
Messages
115
Reaction score
0
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());
?
 

vol7ron

New Member
Messages
434
Reaction score
0
Points
0
I know you can (and should) do that in Perl, is that the correct syntax for PHP?
 

xcaliberse

New Member
Messages
46
Reaction score
0
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.
 

crisp

New Member
Messages
85
Reaction score
0
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')");
 

xcaliberse

New Member
Messages
46
Reaction score
0
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?
 

blobtech

New Member
Prime Account
Messages
17
Reaction score
0
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>
 

brunoais

New Member
Messages
115
Reaction score
0
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:

mrxp_anupam

New Member
Messages
65
Reaction score
0
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:
mysql_select_db("learning_beta", $con);
instead write:
mysql_select_db("learning_beta").
Thats enough.
 
Top