Skip to content

Instantly share code, notes, and snippets.

@stanigator
Created May 3, 2012 23:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stanigator/5345a22b23c1ae98c1d1 to your computer and use it in GitHub Desktop.
Save stanigator/5345a22b23c1ae98c1d1 to your computer and use it in GitHub Desktop.
MySQL database opening problem
<html>
<head>
<title>FORM</title>
</head>
<body>
<form method="post" action="insert.php">
Name: <input type="text" name="name" /><br />
Email: <input type="text" name="email" /><br />
Password: <input type="password" name="password" /><br />
Confirm Password: <input type="password" name="cpassword" /><br />
<input type="submit" name="submit" value="Register" />
<a href="output.php">See data</a>
</form>
</body>
</html>
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$pw = $_POST["password"];
$cpw = $_POST["cpassword"];
if ($name && $email && $pw)
{
mysql_connect("localhost", "root", "") or die("We couldn't connect!");
mysql_select_db("testsite");
mysql_query("INSERT INTO users(name, email, password) VALUES('$name', '$email', '$pw'") or die(mysql_error());
$registered = mysql_affected_rows();
echo "$registered was inserted";
}
else
{
echo "At least one value is empty";
}
mysql_close();
?>
<?php
mysql_connect("localhost", "root", "") or die("Problem with connection");
mysql_select_db("testsite");
$result = mysql_query("SELECT * FROM users");
while($row = mysql_fetch_array($result))
{
echo $row["name"]." ".$row["email"]." ".$row["password"]."<br />";
}
mysql_close();
?>
@stanigator
Copy link
Author

All these files are in the testsite directory under xampp/htdocs/. I installed xampp and ran it before running the form.php in the browser. I made the "testsite" MySQL database, and the "users" table before running form.php (see http://i.imgur.com/pJXdm.png ). However, when I entered all the info properly, mysql_affected_rows() returned -1. This made me think that the database wasn't modified properly.

@willfong
Copy link

willfong commented May 4, 2012

You didn't check for errors in your mysql_query() lines. Try mysql_query( ... ) or die( mysql_error() );

@stanigator
Copy link
Author

stanigator commented May 4, 2012 via email

@willfong
Copy link

willfong commented May 4, 2012

You pasted it!

@stanigator
Copy link
Author

stanigator commented May 4, 2012 via email

@willfong
Copy link

willfong commented May 4, 2012

I have no idea. Why did you leave #mysql? :(

@stanigator
Copy link
Author

stanigator commented May 4, 2012 via email

@willfong
Copy link

willfong commented May 4, 2012

Understood. But you can always try back in #mysql.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment