Jump to content

MYSQL help my database not working or PHP script


Webkiller

Recommended Posts

Heres is what I have put together so far for the PHP script. Below is database screen shot.

 

I would like to hear all ideas. I am stuck as can be so thanks in advance.

 

Michael :)

 

<?php
if($_POST)
{
$name=$_POST['name'];
$email=$_POST['email'];
$comment=$_POST['comment'];

$lowercase = strtolower($email);
 $image = md5( $lowercase );

mysql_connect("localhost","EdwardBrown3236","buckeye3");

//mysql_query("INSERT INTO comments (name,email,comment) VALUES ('$name','$email','$comment') ") ;

}

else { }

?>

 

 

2yki4ie.png

Link to comment
Share on other sites

Assuming the mysql_connect line is creating the connection fine (if not, you need to check your mysql username or password) the second step is to select the database you want to work with. You have to do that before you can insert records.

 

http://www.tizag.com/mysqlTutorial/mysqlconnection.php

 

As a sidenote, make sure you are sanitizing any data that is retrieved from the form. That's one of the key principles of web development -- never trust user input. I'd run any user input through mysql_real_escape_string(), or look into using prepared functions with MySQLi to help prevent security issues.

 

Also, why are you using md5() on the email?

Link to comment
Share on other sites

Assuming the mysql_connect line is creating the connection fine (if not, you need to check your mysql username or password) the second step is to select the database you want to work with. You have to do that before you can insert records.

 

http://www.tizag.com/mysqlTutorial/mysqlconnection.php

 

As a sidenote, make sure you are sanitizing any data that is retrieved from the form. That's one of the key principles of web development -- never trust user input. I'd run any user input through mysql_real_escape_string(), or look into using prepared functions with MySQLi to help prevent security issues.

 

Also, why are you using md5() on the email?

 

Ok thanks for the help. I use md5 encryption for a Apache server. It echoed back on my site that its connected to the host and database. I changed it also to CREATE TABLE comments and still no dice.

 

 

Well the obvious thing i guess is:

 

In your screenshot the table name is "CREATE TABLE comments". That's not matching the table name in your PHP script: "comments".

 

Thanks yeah I missed that. I really thought that was going to fix it hmm.

 

 

 

<?php
if($_POST)
{
$name=$_POST['name'];
$email=$_POST['email'];
$comment=$_POST['comment'];

$lowercase = strtolower($email);
 $image = md5( $lowercase );

mysql_connect("localhost", "-------", "-------") or die(mysql_error());
echo "";
mysql_select_db("-------") or die(mysql_error());
echo "";

//mysql_query("INSERT INTO CREATE TABLE comments (name,email,comment) VALUES ('$name','$email','$comment') ") ;

}

else { }

?>

 

 

The information is just not getting held in the database. Do you see anything wrong with the table screenshot?

Edited by Webkiller
Link to comment
Share on other sites

try renaming the table in your database, rather than altering the code to match. I'm not sure how the spacing will be handled when trying to connect this way.

 

Also, it doesn't look like your selecting the database to write to. try inserting this just before the 'INSERT INTO'

 

mysql_select_db("bestado2_table");

 

In fact, try doing just this, to be assured that your conecting to the Database at all:

 

<?php
mysql_connect("localhost","EdwardBrown3236","buckeye3") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("bestado2_table") or die(mysql_error());
echo "Connected to Database";
?>

 

Once again, I would rename the table in the database to 'comments' rather than 'CREATE TABLE comments' as this will probably cause headaches and confusion down the road. (If not already).

Link to comment
Share on other sites

try renaming the table in your database, rather than altering the code to match. I'm not sure how the spacing will be handled when trying to connect this way.

 

Also, it doesn't look like your selecting the database to write to. try inserting this just before the 'INSERT INTO'

 

mysql_select_db("bestado2_table");

 

In fact, try doing just this, to be assured that your conecting to the Database at all:

 

<?php
mysql_connect("localhost","EdwardBrown3236","buckeye3") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("bestado2_table") or die(mysql_error());
echo "Connected to Database";
?>

 

Once again, I would rename the table in the database to 'comments' rather than 'CREATE TABLE comments' as this will probably cause headaches and confusion down the road. (If not already).

 

 

Everything is fine connecting to Local Host and database like I posted in the previous post. It echoed back to me on my site saying it was connected. I will try and rename the table in the database to see if that works. The ---- was just for this site so everyone didnt see it.

 

I should also point out, just in case, that the two "//" on this line:

 

//mysql_query("INSERT INTO CREATE TABLE comments (name,email,comment) VALUES ('$name','$email','$comment') ") ;

 

turn that line into a comment, meaning that line never actually runs. If you need it to work, you'll need to remove "//".

 

Yeah I tryed that already and nothing works. With the // its hidden text.

Edited by Webkiller
Link to comment
Share on other sites

The words: CREATE TABLE are functionality/reserved words in SQL, so if they are contained in an SQL string, they will be treated as such.

 

Therefore "INSERT INTO CREATE TABLE comments" is never going to be a valid SQL string. Best solution would be to just rename your database.

Edited by BeeDev
Link to comment
Share on other sites

last thing i would try is quoting the table name. change this line:

 

//mysql_query("INSERT INTO CREATE TABLE comments (name,email,comment) VALUES ('$name','$email','$comment') ") ;

 

to this

mysql_query("INSERT INTO `CREATE TABLE comments` (name,email,comment) VALUES ('$name','$email','$comment')");

 

note the backtick '`' rather than using quoting the table name.

 

Yeah I tryed that already and nothing works. With the // its hidden text.

 

You know that this means that it is commented out and this line will not be executed with the '//' in front? The slashes in front means this line never gets called.

Link to comment
Share on other sites

Beedev you were right I needed to change something and it was the table. I renamed the table not the database.

 

Now everything works fine. The wording of the last table in the database above would not make it function.

 

NEW PROBLEM

 

I see the posts being recoded in the database but each refresh of the page on the site its not holding each person comments.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...