Jump to content

Archived

This topic is now archived and is closed to further replies.

Hellusius

Made database but add does not work

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

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
Oops  :-[, sorry I did that, thank you for editing it, going to try the code now.

P.S, hope you don't mind me asking, but what is the thing

<?php
}
?>

on the bottom for?

Share this post


Link to post
Share on other sites
It closes off the loop that begins with else { just ahead of the form.

The logic flow is basically this:

IF the submit button was pressed

{ process the data and stick it in the database

} else {

show me the form

}

Share this post


Link to post
Share on other sites
ok that made sense, starting to understand.

anyway, I get a blanco page, but how can I see if anything is added to the database?

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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]

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
now I get an error on the line
[code]mysql_select_db($db_name) or die(" Error selecting database: ". mysql_error()); // any problems?[/code]

which is

[code]Parse error: syntax error, unexpected T_STRING in /home/www/warnicro.awardspace.com/postit.html on line 16[/code]

Share this post


Link to post
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 ;

Share this post


Link to post
Share on other sites
think I got it, you wrote "of" instead of "or"

Edit: got some result :D

[code]Rows  1
Row length ø 20
Row size  ø 2,068 Bytes
Next Autoindex 2
Creation Sep 02, 2006 at 12:49 AM
Last update Sep 02, 2006 at 10:00 PM
Last check Sep 02, 2006 at 06:49 PM[/code]

Share this post


Link to post
Share on other sites

×

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.