Jump to content

Please Help I Keep Getting An Error With My Php


Joey46

Recommended Posts

This is my PHP code below$CourseNumber = $_POST['CourseNumber'];$CourseName = $_POST['CourseName'];$StudyMode = $_POST['StudyMode'];// assemble the sql query string to insert a record$query = "INSERT INTO Courses(CourseNumber, CourseName, StudyMode,) VALUES ('CourseNumber', '$CourseName', '$StudyMode')"; // open a connection and select the database$conn = mysql_connect($host, $username, $password);@mysql_select_db($database, $conn) or die( "Unable to select $database database"); // add the recordif(mysql_query($query, $conn)){ echo "Record added successfully!";}else{ echo "Something went wrong. MySQL reports: <b>", mysql_error(), "</b>";} mysql_close($conn); // reuse the previous forminclude("Courseadd.htm");?>This is my HTML code<h2 align="center">Add Course</h2> <form method="post" action="Courseadd.php"><table width="450" border="5" align="center" cellpadding="5" bordercolor="#CCCCCC"> <tr> <td width="150">CourseNumber</td> <td width="300"><input type="text" name="CourseNumber_ID" /> </td> </tr> <tr> <td>CourseName </td> <td><input type="text" name="CourseName" /></td> </tr> <tr> <td>StudyMode</td> <td><input type="text" name="StudyMode" /></td> </tr> <tr> <td><input type="reset" name="Reset" value="Cancel" /></td> <td><input type="submit" name="Submit" value="Add Course" /></td> </tr></table> </form><p> </p><div align="center"> </div></form>This is the ERROR MSGSomething went wrong. MySQL reports: 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 ') VALUES ('CourseNumber', 'Diploma', 'flex')' at li

Link to comment
Share on other sites

You need to change the variable as below, had not included the $ for CourseNumber

// assemble the sql query string to insert a record$query = "INSERT INTO Courses(CourseNumber, CourseName, StudyMode,)VALUES ('$CourseNumber', '$CourseName', '$StudyMode')";

Link to comment
Share on other sites

You need to change the variable as below, had not included the $ for CourseNumber
// assemble the sql query string to insert a record$query = "INSERT INTO Courses(CourseNumber, CourseName, StudyMode,)VALUES ('$CourseNumber', '$CourseName', '$StudyMode')";

You were right of course but when i tired it after fixing it i still get this errorSomething went wrong. MySQL reports: 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 ') VALUES ('', 'Diploma', 'flex')' at line 1
Link to comment
Share on other sites

Most likely, being a number field, you shouldn't have quotes around $CourseNumber in the query.
This is my php script now$CourseNumber = $_POST['CourseNumber'];$CourseName = $_POST['CourseName'];$StudyMode = $_POST['StudyMode'];// assemble the sql query string to insert a record$query = "INSERT INTO Courses (CourseNumber, CourseName, StudyMode,)"; "VALUES ($CourseNumber, '$CourseName', '$StudyMode')"; // open a connection and select the database$conn = mysql_connect($host, $username, $password);@mysql_select_db($database, $conn) or die( "Unable to select $database database"); // add the recordif(mysql_query($query, $conn)){ echo "Record added successfully!";}else{ echo "Something went wrong. MySQL reports: <b>", mysql_error(), "</b>";} mysql_close($conn);This is now the error msgSomething went wrong. MySQL reports: 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 ')' at line 1
Link to comment
Share on other sites

You have an extra comma in the query. Anyways, you haven't added the "VALUES" part into the query. Here's what it should be like:

$query = "INSERT INTO Courses (CourseNumber, CourseName, StudyMode) VALUES ($CourseNumber, '$CourseName', '$StudyMode')";

Link to comment
Share on other sites

You have an extra comma in the query. Anyways, you haven't added the "VALUES" part into the query. Here's what it should be like:
$query = "INSERT INTO Courses (CourseNumber, CourseName, StudyMode) VALUES ($CourseNumber, '$CourseName', '$StudyMode')";

This is what i am getting back nowSomething went wrong. MySQL reports: 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 ' 'diploma', 'flex')' at line 1 so then i played around with it and it says added now so it is fixed thankyou for your help much appreciated mate
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...