I have 3 fieldnames called ID, Autore, Titolo in Mysql. The tablename is demo and database name is forms1
I have problem to insert data from many forms into database.
I test code but i have something wrong into insert.php
Form Code:
<style>
br {margin-bottom:-10px;}
</style>
<form action="insert.php" method="post">
<b>Titolo </b> <input type="text" name="Titolo"><br><br>
<b>Autore </b><input type="text" name="Autore"><br><br>
<input type="Submit">
</form>
Insert.php
<?php
define('DB_NAME', 'forms1');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
$sql = "INSERT INTO demo VALUES
('', 'Autore', 'Titolo')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
mysql_close();
?>
This piece of code is wrong:
$sql = "INSERT INTO demo VALUES
('', 'Autore', 'Titolo')";
When i try insert values like Jack, John from form the values that are adding are Autore and Titolo
But Autore & Titolo are my 2nd-3rd fieldnames in my demo table, but its are added like values!
This is my ResultPage.php
<html>
<?php
echo "<h2>Lista Autori</h2>";
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='auto' align='center'>N</td>";
echo "<td width='auto' > Nome</td>";
echo "</tr>";
//1. Connessione al Database
$connection = mysql_connect("localhost","root","");
if(!$connection){
die("Database connection failed: " . mysql_error());
}
//2. Seleziona Database
$db_select = mysql_select_db("forms1",$connection);
if (!$db_select) {
die("Database connection failed: " . mysql_error());
}
//3. Interroga Database
$result = mysql_query("SELECT * FROM demo LIMIT 0, 30 ", $connection);
if (!$result) {
die("Database query failed: " . mysql_error());
}
//4. Use Returned Data
while ($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='auto'>" . $row[1] . "</td>";
echo "<td width='auto'>" . " " . $row[2] . " " . "</td>";
echo "</tr>";
}
mysql_close($connection);
?>
</html>
How can i change insert.php file?