Jump to content


Photo

Made database but add does not work


  • Please log in to reply
12 replies to this topic

#1 Hellusius

Hellusius
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 02 September 2006 - 11:55 PM

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

<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);
?>

Did I forget something, cause nothing is being added to the database in my oppinion

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 03 September 2006 - 12:03 AM

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

<?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
}
?>

Legend has it that reading the manual never killed anyone.
My site

#3 Hellusius

Hellusius
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 03 September 2006 - 12:07 AM

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?

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 03 September 2006 - 12:18 AM

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

}
Legend has it that reading the manual never killed anyone.
My site

#5 Hellusius

Hellusius
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 03 September 2006 - 12:21 AM

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?

#6 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 03 September 2006 - 12:29 AM

Change:
   $result = mysql_query($query);
} else {

To:
   $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 {

You'll need to use whatever tools you usually need to view the database to see if (that) the data were added.
Legend has it that reading the manual never killed anyone.
My site

#7 Hellusius

Hellusius
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 03 September 2006 - 01:03 AM

bumped into a new problem now

<?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
}
?>

it gave me this error

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


I know its something with that line, but I can't see what, any help is very much appreciated

#8 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 03 September 2006 - 01:33 AM

Check again your database 'details' is it there ?

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#9 eRott

eRott
  • Members
  • PipPipPip
  • Advanced Member
  • 206 posts
  • LocationToronto, ON

Posted 03 September 2006 - 01:59 AM

Try this:

<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>

As for the database, use this to create it:

create_db.php
<?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);
?>


#10 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 03 September 2006 - 02:22 AM

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:

// 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?

Legend has it that reading the manual never killed anyone.
My site

#11 Hellusius

Hellusius
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 03 September 2006 - 02:33 AM

now I get an error on the line
mysql_select_db($db_name) or die(" Error selecting database: ". mysql_error()); // any problems?

which is

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


#12 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 03 September 2006 - 02:39 AM

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 ;
Legend has it that reading the manual never killed anyone.
My site

#13 Hellusius

Hellusius
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 03 September 2006 - 03:00 AM

think I got it, you wrote "of" instead of "or"

Edit: got some result :D

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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users