Reputation: 193
index.php
:
<form action="insert.php" method="POST">
Comments: <input type="text" name="field1_name"/>
<input type="Submit" /></form>
This takes in user input and I want whatever the user inputs to be stored in a database that I have using MAMP's phpmyadmin.
insert.php
:
$user = 'x';
$password = '';
$db = 'comment_schema';
$host = 'localhost';
$port = 3306;
$field1_name = ($_POST['field1_name']);
$mysqli = new mysqli('localhost',$user,$password,$db,$port);
$mysqli->query("INSERT INTO parentComment(ID,Comment) VALUES('ID','$field1_name')");
The database table I have is named parentComment
and the two columns are ID
and Comment
. I have ID
set that so it automatically increments in my database.
When I try typing in something in my comments form, it gets updated in my database because a new row shows up with an ID
however the comment doesn't get stored. So, it's doing something but it's not storing the comment for some reason. Not sure where I'm going wrong.
That's how my parentComment table looks like. The current error I'm getting is:
A fatal JavaScript error has occurred. Would you like to send an error report?
I reported the error though so it hasn't been showing up. I don't know if that's part of the problem.
Upvotes: 1
Views: 2268
Reputation: 91
And for the javascript error its only in chrome I also had that error message ("A fatal JavaScript error has occurred. Would you like to send an error report?") in Chrome browser. I tried 'localhost/phpmyadmin' in Internet Explorer, it work well. So disable PageXray extension in Chrome browser from help of stackoverflow. and it worked for me
Upvotes: 0
Reputation: 481
If you maked ID auto increment you don't need to assign its value.Try this-
$field1_name = $_POST['field1_name'];
$mysqli->query("INSERT INTO parentComment(id,Comment) VALUES('','$field1_name')");
Upvotes: 0
Reputation: 607
Edit: Sorry I believe my statement below is incorrect.
I believe there is at least an error in your query where you are using the $field1_name
variable. You are treating it the same as the ID (string) when it is a variable.
$mysqli->query("INSERT INTO parentComment(ID,Comment) VALUES('ID','.$field1_name.')");
Whether this will fix your issue or not I am not entirely sure.
Upvotes: 0
Reputation: 2817
As you said, ID
field value is autogenerated so you have no need trying to declare it explicitly in the insert statement. So you should change insert query to this:
$mysqli->query("INSERT INTO parentComment(Comment) VALUES('$field1_name')");
Upvotes: 1