Jump to content

[SOLVED] PHP / SQL adding to table question


reverendrazorback

Recommended Posts

This is a simple code to add data to a table. When I use this code - the data is inserted fine:

 

<html>

<head>

<title>Add New Record in MySQL Database</title>

</head>

<body>

<?php

if(isset($_POST['add']))

{

$dbhost = '******';

$dbuser = '******

$dbpass = '******

$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn )

{

  die('Could not connect: ' . mysql_error());

}

 

if(! get_magic_quotes_gpc() )

{

  $Book_Title = addslashes ($_POST['Book_Title']);

  $Author = addslashes ($_POST['Author']);

  $Category = addslashes ($_POST['Category']);

  $Position = addslashes ($_POST['Position']);

}

else

{

  $Book_Title = $_POST['Book_Title'];

  $Author = $_POST['Author'];

  $Category = $_POST['Category'];

  $Position = $_POST['Position'];

}

$Submission_Date = $_POST['Submission_Date'];

 

$sql = "INSERT INTO MasterList ".

      "(Book_Title,Author,Category,Position, Submission_Date) ".

      "VALUES ".

      "('$Book_Title','$Author','$Category','$Position','$Submission_Date')";

mysql_select_db('library');

$retval = mysql_query( $sql, $conn );

if(! $retval )

{

  die('Could not enter data: ' . mysql_error());

}

echo "Entered data successfully

";

mysql_close($conn);

}

else

{

?>

<form method="post" action="<?php $_PHP_SELF ?>">

<table width="600" border="0" cellspacing="1" cellpadding="2">

<tr>

<td width="250">Book Title</td>

<td>

<input name="Book_Title" type="text" id="Book_Title">

</td>

</tr>

<tr>

<td width="250">Book Author</td>

<td>

<input name="Author" type="text" id="Author">

</td>

</tr>

<tr>

<td width="250">Category</td>

<td>

<input name="Category" type="text" id="Category">

</td>

</tr>

<tr>

<td width="250">Position</td>

<td>

<input name="Position" type="text" id="Position">

</td>

</tr>

<tr>

<td width="250">Submission Date [ yyyy-mm-dd ]</td>

<td>

<input name="Submission_Date" type="text" id="Submission_Date">

</td>

</tr>

<tr>

<td width="250"> </td>

<td> </td>

</tr>

<tr>

<td width="250"> </td>

<td>

<input name="add" type="submit" id="add" value="Add Book to Library">

</td>

</tr>

</table>

</form>

<?php

}

?>

</body>

</html>

 

 

 

This coding works fine, I get this to show that it worked, and the actual data is in the tables correctly:

 

Entered data successfully 

 

 

However, when I replace Position with Group, it will not work.

 

<html>

<head>

<title>Add New Record in MySQL Database</title>

</head>

<body>

<?php

if(isset($_POST['add']))

{

$dbhost = '******';

$dbuser = '******';

$dbpass = '******';

$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn )

{

  die('Could not connect: ' . mysql_error());

}

 

if(! get_magic_quotes_gpc() )

{

  $Book_Title = addslashes ($_POST['Book_Title']);

  $Author = addslashes ($_POST['Author']);

  $Category = addslashes ($_POST['Category']);

  $Group = addslashes ($_POST['Group']);

}

else

{

  $Book_Title = $_POST['Book_Title'];

  $Author = $_POST['Author'];

  $Category = $_POST['Category'];

  $Group = $_POST['Group'];

}

$Submission_Date = $_POST['Submission_Date'];

 

$sql = "INSERT INTO MasterList ".

      "(Book_Title,Author,Category,Group, Submission_Date) ".

      "VALUES ".

      "('$Book_Title','$Author','$Category','$Group','$Submission_Date')";

mysql_select_db('library');

$retval = mysql_query( $sql, $conn );

if(! $retval )

{

  die('Could not enter data: ' . mysql_error());

}

echo "Entered data successfully

";

mysql_close($conn);

}

else

{

?>

<form method="post" action="<?php $_PHP_SELF ?>">

<table width="600" border="0" cellspacing="1" cellpadding="2">

<tr>

<td width="250">Book Title</td>

<td>

<input name="Book_Title" type="text" id="Book_Title">

</td>

</tr>

<tr>

<td width="250">Book Author</td>

<td>

<input name="Author" type="text" id="Author">

</td>

</tr>

<tr>

<td width="250">Category</td>

<td>

<input name="Category" type="text" id="Category">

</td>

</tr>

<tr>

<td width="250">Group</td>

<td>

<input name="Group" type="text" id="Group">

</td>

</tr>

<tr>

<td width="250">Submission Date [ yyyy-mm-dd ]</td>

<td>

<input name="Submission_Date" type="text" id="Submission_Date">

</td>

</tr>

<tr>

<td width="250"> </td>

<td> </td>

</tr>

<tr>

<td width="250"> </td>

<td>

<input name="add" type="submit" id="add" value="Add Book to Library">

</td>

</tr>

</table>

</form>

<?php

}

?>

</body>

</html>

 

 

This code gives me this error:

Could not enter data: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Group, Submission_Date) VALUES ('testing','tester','test cat','test grouping','2' at line 1

 

P.S. I love this forum - and the great helpful coders out there on it. Thanks for all of the help!

Link to comment
https://forums.phpfreaks.com/topic/141011-solved-php-sql-adding-to-table-question/
Share on other sites

Sure:

 

Field Type Collation Attributes Null Default Extra Action

  Book_Title varchar(10000) latin1_swedish_ci  Yes NULL               

  Author varchar(2000) latin1_swedish_ci  Yes NULL               

  Category varchar(5000) latin1_swedish_ci  Yes NULL               

  Group varchar(5000) latin1_swedish_ci  Yes NULL               

  Group_2 varchar(5000) latin1_swedish_ci  Yes NULL               

  ISBN/LC varchar(5000) latin1_swedish_ci  Yes NULL               

  Position varchar(5000) latin1_swedish_ci  Yes NULL               

  Edition varchar(5000) latin1_swedish_ci  Yes NULL               

  Notes blob  BINARY Yes NULL               

  Submission_Date varchar(500) latin1_swedish_ci  Yes NULL               

 

Maybe I should have better displayed the scheme:

 

Field        Type            Collation              Null        Default 

Book_Title varchar(10000) latin1_swedish_ci  Yes        NULL               

Author      varchar(2000) latin1_swedish_ci  Yes        NULL               

Category  varchar(5000) latin1_swedish_ci  Yes        NULL               

Group      varchar(5000) latin1_swedish_ci  Yes        NULL               

Group_2    varchar(5000) latin1_swedish_ci  Yes        NULL               

ISBN/LC    varchar(5000) latin1_swedish_ci  Yes        NULL               

Position    varchar(5000) latin1_swedish_ci  Yes        NULL               

Edition      varchar(5000) latin1_swedish_ci  Yes        NULL               

Notes      blob              BINARY                Yes        NULL               

Submission_Date varchar(500) latin1_swedish_ci  Yes NULL               

 

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.