1

I have connected my website to my database successfully when i submit the form it goes through but nothing is getting inserted into the database.

Code Below:

    <?php
if( $_POST )
{
  $con = mysql_connect("server","user","pass");

  if (!$con)
  {
    die('Could not connect: ' . mysql_error());
  }

  mysql_select_db("buycruisesdb", $con);

  $users_name = $_POST['name'];
  $users_email = $_POST['email'];

  $users_name = mysql_real_escape_string($users_name);
  $users_email = mysql_real_escape_string($users_email);

  $query = "
  INSERT INTO `website_subscribers`(`name_sub`, `email_sub`) VALUES ([$users_name],[$users_email])";

  mysql_query($query);

  echo "<h2>Thank you for subscribing!</h2>";
  echo $query;
  echo $users_name;
  echo $users_email;

  mysql_close($con);
}
?>

buycruisesdb = database
website_subscribers = table inside the database
name_sub/email_sub = columns inside the table

the form html is below:

!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
    <form action="php/subscriber.php" id="form" method="post" name="form">
       <input id="name_sub" name="name" placeholder="Name" type="text">
       <input id="email_sub" name="email" placeholder="Email" type="text">
       <input type="submit" value="Submit" name="f_submit">
    </form>
</body>
</html>

Not sure exactly why this is not inputing anyone have an idea?

it says that it is inserting the proper values and into the proper tables Image

Hamza Zafeer
  • 2,360
  • 13
  • 30
  • 42
Miessau
  • 21
  • 5
  • Is it maybe because i have to direct the INSERT to buycruisesdb>website_subscribers>columns? – Miessau Apr 22 '16 at 20:01
  • is it possible that spaces in the username, You must Quote strings: INSERT INTO .... VALUES ('[$users_name]','[$users_email]')"; – Bernd Buffen Apr 22 '16 at 20:06
  • Print your query $query = " INSERT INTO `website_subscribers`(`name_sub`, `email_sub`) VALUES ([$users_name],[$users_email])"; and execute in your dB directly. If its working there then, it should work here also. – Aparna Apr 22 '16 at 20:10
  • @Aparna Yea thats the wierd part if i execute it in the query for the db it goes in... maybe connection... I have updated the post with a image of the query and the values echoed – Miessau Apr 22 '16 at 20:15
  • **Warning**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) which has been **removed** entirely from the latest version of PHP. You should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Apr 27 '16 at 14:58

2 Answers2

1

Square brackets are not valid in MySQL queries. You should be using quotes around the strings.

$query = "INSERT INTO `website_subscribers` (`name_sub`, `email_sub`) VALUES ('$users_name', '$users_email')";
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

change it to:

$query = "
  INSERT INTO website_subscribers (name_sub,email_sub) VALUES ('".$users_name."','".$users_email."') ";

just copy the code and try it out