DeemStarr DeemStarr - 4 months ago 16
MySQL Question

Values are not being inserted in SQL database

I cannot figure out why the values from this PHP file are not being inserted into my SQL database. The code is nearly identical to a working PHP file I am using for a different table. Code is as follows:

<?php
mysql_connect("xxx","xxx","xxx");
mysql_select_db("andrew_history");
$Name=$_POST['Name'];
$Year=$_POST['Year'];
$Press=$_POST['Press'];
$Description = $_POST['Description'];
$Title = $_POST['Title'];


$dbLink = mysql_connect("xxxx", "xxxx", "xxxxxx");
mysql_query("SET character_set_client=utf8", $dbLink);
mysql_query("SET character_set_connection=utf8", $dbLink);

if($submit)
{
if($Name&&$Year&&$Press&&$Description&&$Title)
{
$insert=mysql_query("INSERT INTO historytable (Name,Year,Press,Description,Title) VALUES ('$Name','$Year','$Press', '$Description', '$Title') ");
echo "<meta HTTP-EQUIV='REFRESH' content='0; url=history.php'>";
}
else
{
echo "please fill out all fields";
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>History</title>
</head>

<body>
<center>
<form action="history.php" method="POST">
<table>
<tr><td>Name: <input type="text" name="Name"/></td></tr>
<tr><td>Year: <input type="text" name="Year"/></td></tr>
<tr><td>Press: <input type="text" name="Press"/></td></tr>
<tr><td>Title: <input type="text" name="Title"/></td></tr>
<tr><td colspan="2">Description: </td></tr>
<tr><td colspan="5"><textarea name="Description" rows="10" cols="50"></textarea></td></tr>
<tr><td colspan="2"><input type="submit" name="submit" value="Description"></td></tr>
</table>
</form>

<?php
$dbLink = mysql_connect("xxxx", "xxxx", "xxxxx");
mysql_query("SET character_set_results=utf8", $dbLink);
mb_language('uni');
mb_internal_encoding('UTF-8');


$getquery=mysql_query('SELECT * FROM historytable ORDER BY id DESC');


while($rows=mysql_fetch_assoc($getquery))
{
$id=$rows['id'];
$Name=$rows['Name'];
$Year=$rows['Year'];
$Press=$rows['Press'];
$Description=$rows['Description'];
$Title=$rows['Title'];
echo $Name . $Year . '<br/>' . '<br/>' . $Press . '<br/>' . '<br/>' . $Description . $Title;
}
?>

</body>
</html>


Link to site: http://aswanson.net/Work/history.php

Answer

if($submit) here is the mistake.you are not set any $submit variable yet.please check below code.

<?php
mysql_connect("xxx","xxxx","xxxx");
mysql_select_db("andrew_history");
$Name=$_POST['Name'];
$Year=$_POST['Year'];
$Press=$_POST['Press'];
$Description = $_POST['Description'];
$Title = $_POST['Title'];

$dbLink = mysql_connect("xxxx", "xxxx", "xxxx");
mysql_query("SET character_set_client=utf8", $dbLink);
mysql_query("SET character_set_connection=utf8", $dbLink);

if(isset($_POST['submit']))
{
   if(!empty($Name) && !empty($Year) && !empty($Press) && !empty($Description) && !empty($Title))
  {
    $insert=mysql_query("INSERT INTO historytable (Name,Year,Press,Description,Title) VALUES ('$Name','$Year','$Press', '$Description', '$Title') ");
    echo "<meta HTTP-EQUIV='REFRESH' content='0; url=history.php'>";
}
else
{
    echo "please fill out all fields";
}
}
?>