Jump to content

Made database but add does not work


Hellusius

Recommended Posts

I made a database with a few things, just to take a peak at how things work, but I already stranded, I made a database, which I connected to and I made a few forms so you can insert a row into the database, this is the coding

[code]<form action = "/postit.html" method = "post">
Enter your name: <br />
<input type = "text" size = "40" name = "name" /><br />
Enter your age: <br />
<input type = "text" size = "3" name = "age" /><br />
Enter your sex: <br />
<input type = "text" size = "1" name = "sex" /><br /><br />
<input type = "submit" value = "Add Data" />
</form>
<?php
// collect data sent from form
$name = $_POST['name'];
$age = $_POST['age'];
$sex = $_POST['sex'];

// connection variables
$host = "*****"; // almost inavariably
$db_name = "*****";
$db_user = "*****";
$db_pass = "*****";
$db_table = "details";

// connect to host and select db
mysql_connect($host, $db_user, $db_pass);
mysql_select_db($db_name);

// create and execute the query to insert data
$query = "INSERT INTO $db_table (id, name, age, sex) VALUES ('', '$name' , '$age' , '$sex')";
$result = mysql_query($query);
?>[/code]

Did I forget something, cause nothing is being added to the database in my oppinion
Link to comment
Share on other sites

Similar but notice how the flow of the script goes. (btw - don't post your real db details. I edited them for you).

[code]<?php
if (isset($_POST['submit'])) {
  // collect data sent from form but ONLY if it was submitted
  $name = $_POST['name'];
  $age = $_POST['age'];
  $sex = $_POST['sex'];

  // connection variables
  $host = "*****"; // almost inavariably
  $db_name = "details";
  $db_user = "*****";
  $db_pass = "*****";
  $db_table = "details";

  // connect to host and select db
  mysql_connect($host, $db_user, $db_pass);
  mysql_select_db($db_name);

  // create and execute the query to insert data
  $query = "INSERT INTO $db_table (id, name, age, sex) VALUES ('', '$name' , '$age' , '$sex')";
  $result = mysql_query($query);
} else {
?>

<form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "post">
Enter your name: <br />
<input type = "text" size = "40" name = "name" /><br />
Enter your age: <br />
<input type = "text" size = "3" name = "age" /><br />
Enter your sex: <br />
<input type = "text" size = "1" name = "sex" /><br /><br />
<input type = "submit" name = "submit" value = "Add Data" />
</form>


<?php
}
?>[/code]
Link to comment
Share on other sites

Change:
[code]  $result = mysql_query($query);
} else {[/code]

To:
[code]  $result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query); // in case it bombs
  echo "Thanks. The information was added."
  // or whatever else you want to do
} else {[/code]

You'll need to use whatever tools you usually need to view the database to see if (that) the data were added.
Link to comment
Share on other sites

bumped into a new problem now

[code]<?php
if (isset($_POST['submit'])) {
  // collect data sent from form but ONLY if it was submitted
  $name = $_POST['name'];
  $age = $_POST['age'];
  $sex = $_POST['sex'];

  // connection variables
  $host = "******"; // almost inavariably
  $db_name = "details";
  $db_user = "******";
  $db_pass = "******";
  $db_table = "details";

  // connect to host and select db
  mysql_connect($host, $db_user, $db_pass);
  mysql_select_db($db_name);

  // create and execute the query to insert data
  $query = "INSERT INTO $db_table (id, name, age, sex) VALUES ('', '$name' , '$age' , '$sex')";
  $result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query); // in case it bombs
?>
<center>
<table width="500" bgcolor="000000" cellspacing="0" cellpadding="0" align="center">
  <tr>
    <td>
<font color="1055AA" size="3">Infromation has been added, <a href="/index.php">click here</a> to return to the homepage</font>
    </td>
  </tr>
</table>
</center>
<?php
} else {
?>

<form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "post">
Enter your name: <br />
<input type = "text" size = "40" name = "name" /><br />
Enter your age: <br />
<input type = "text" size = "3" name = "age" /><br />
Enter your sex: <br />
<input type = "text" size = "1" name = "sex" /><br /><br />
<input type = "submit" name = "submit" value = "Add Data" />
</form>


<?php
}
?>[/code]

it gave me this error

[quote]Error: No database selected with query INSERT INTO details (id, name, age, sex) VALUES ('', 'test' , '00' , 'v')[/quote]

I know its something with that line, but I can't see what, any help is very much appreciated
Link to comment
Share on other sites

Try this:

[code]
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?
if(isset($_POST['add']))
{
$dbhost = '$dbhost';
$dbuser = '$dbuser';
$dbpass = '$dbpass';
$dbname = '$dbname';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
$db = mysql_select_db($dbname, $conn) or die(mysql_error());

$name = $_POST['name'];
$age = $_POST['age'];
$sex = $_POST['sex'];

$query = "INSERT INTO information (name, age, sex) VALUES ('$name', '$age', '$sex')";
mysql_query($query) or die('Error, insert query failed');

mysql_close($conn);
echo "New entry added!";
}
else
{
?>
<form method="post">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">Name</td>
<td><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td width="100">Age</td>
<td><input name="age" type="text" id="age"></td>
</tr>
<tr>
<td width="100">Sex</td>
<td><input name="sex" type="text" id="sex" size = "1"></td>
</tr>
<tr>
<td width="100">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="100">&nbsp;</td>
<td><input name="add" type="submit" id="add" value="Add Data"></td>
</tr>
</table>
</form>
<?
}
?>
</body>
</html>
[/code]

As for the database, use this to create it:

create_db.php
[code]
<?php
$dbhost = '$dbhost';
$dbuser = '$dbuser';
$dbpass = '$dbpass';
$dbname = '$dbname';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
$db = mysql_select_db($dbname, $conn) or die(mysql_error());

$query  = 'CREATE DATABASE contacts';
$result = mysql_query($query);

mysql_select_db('contacts') or die('Cannot select database');

$query = 'CREATE TABLE information( '.
        'id INT NOT NULL AUTO_INCREMENT, '.
        'name VARCHAR(50) NOT NULL, '.
        'age VARCHAR(3) NOT NULL, '.
        'sex VARCHAR(6) NOT NULL, '.
        'PRIMARY KEY(id))';

$result = mysql_query($query);

mysql_close($conn);
?>
[/code]
Link to comment
Share on other sites

The query you had is fine. The error it displays suggests that there was a problem establishing a real connection.  Usually a database name is something like username_databasenameyouchose rather than just the databasenameyouchose. So carefully check the values you're using when you define the database conn variables (which you shouldn't post here but obviously you need in your working script on your server).

Let's take the error trapping a little further.  Adjust the connection lines in the script to these:

[code]// connect to host and select db
  mysql_connect($host, $db_user, $db_pass) of die("Error connecting: ". mysql_error()); // any problems?
  mysql_select_db($db_name) or die(" Error selecting database: ". mysql_error()); // any problems?[/code]
Link to comment
Share on other sites

The syntax error message relates to the line where the php interpreter found something it couldn't handle not necessarily that the error exists on the line number it gives.  Carefully check to be sure that quotes are matched in all the preceding lines, and that statements all end properly, i.e. you don't have $db_name = "details ... without the closing quote or the closing ;
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.