Jump to content

[SOLVED] inserting data


ali_b

Recommended Posts

hi, This is my first time trying to do this so dont laugh :P. i found a tutorial how to insert select and view data from a database but i cant get it to work, it says that the data has been insterted but when i go to index.php nothing is shown, maybe it is my database? wht should the code be to make a suitable database?

 

Here is the config.php to connect to the database

 

<?
$dbh=mysql_connect ("localhost", "<my username>", "<my password>") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("menugenc_clients"); 
?>

 

Here is the forms.php where i insert the data:

 

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

<body>
<form name="form1" method="post" action="insert.php">
<p>Name:-<br>
<input name="name" type="text" id="name">
<br>
<br>
Age:-<br>
<input name="age" type="text" id="age">
<br>
<br>
<input type="submit" name="Submit">
</p>
</form>
</body>
</html>

 

this then gets linked to insert.php

 

<?php

// Include the Mysql Connecting file (config.php)
include "config.php"; // corrected by army, there was no ";" at the end, so the INCLUDE-function was not closed, and you got an error f you tried to run it. Now it is fixed and woks well.

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

//Insert into Mysql Database:
mysql_query("INSERT INTO test (name,age) VALUES( " . $name . ", " . $age . ")"); 

// Show a alert when data inserted to Mysql.
echo "<script language=javascript>alert('Data inserted to Mysql Databse!'); window.location = 'forms.php'; </script>";
?>

 

it then says that my data has been inserted and i go to index.php which is this:

 

<?php

// Include Mysql Connecting file (config.php)
include "config.php";

// Selecting data from Database:
// Tip: Asteric (*) is used to select all the rows in the table name.
$result = mysql_query("SELECT * FROM test");
while ($show = mysql_fetch_array($result)) {

// Write the Name and Age inserted into Mysql Database
echo "Name: " . $show['name'];
echo "Age: " . $show['age'];

}

?>

 

but it is just a blank page :(

 

can you see what is wrong with it?

 

Cheers in advance,

Ali

Link to comment
https://forums.phpfreaks.com/topic/45155-solved-inserting-data/
Share on other sites

Archived

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

×
×
  • 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.