Jump to content

Recommended Posts

I have created a php insert to add data input from a html form into a phpmyadmin database runnig in xxamp.

 

php connect file works fine

<?php
$connect = mysql_connect("localhost","root","") or die("Connection error");

mysql_select_db("ria") or die(mysql_error());

?>

 

php code in insert data into database not working

<?php 
if (isset($_POST['required'])) {
include ('connection.php');

$title = $_POST['title'];
$cost = $_POST['cost'];
$stock = $_POST['no in stock'];
$insert = "INSERT INTO book (title, cost, no in stock) VALUES ('$title', '$cost', '$stock')";

if (!mysqli_query($connect, $insert)) {
 	die('can not insert new record');
}
$newrecord = "New record added";	
															  
}
?>

 

HTML forms

 <form action="Insert.php">

<input type="hidden" name="required" value="true"  />

<fieldset>

<legend>Insert New Book</legend>

<label>Title

<input type="text" name="title" /></label>

<label>Cost

<input type="text" name="cost" /></label>

<label>No. in Stock

<input type="text" name="no in stock" />

</label>

</fieldset>
<br />

<input type="submit" value="Submit" />

</form>

last bit of php to give a message when data has been added

<?php echo $newrecord
?>

 

any suggestions what is wrong with the php.

Link to comment
https://forums.phpfreaks.com/topic/224543-mysql-insert-error/
Share on other sites

Assuming you're getting the 'can not insert new record' error, change it so errors are reported, and the query string is echoed.

 

<?php 
if (isset($_POST['required'])) {
include ('connection.php');
$title = $_POST['title'];
$cost = $_POST['cost'];
$stock = $_POST['no in stock'];
$insert = "INSERT INTO book (title, cost, no in stock) VALUES ('$title', '$cost', '$stock')";
if ( !mysqli_query($connect, $insert) ) {
	die( "<br>Query string: $insert<br>Failed with error: " . mysqli_error($connect) );
} else {
	if( mysqli_affected_rows($connect) > 0 ) {
		$newrecord = "New record added";
	}
}
}
?>

[/code]

Link to comment
https://forums.phpfreaks.com/topic/224543-mysql-insert-error/#findComment-1159886
Share on other sites

I couldn't get any of it to work because i forgot to add method="post" to the html form.

Now that I have the php script will run and i get a

Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in F:\xampp\htdocs\RIA\insert.php on line 18

Warning: mysqli_error() expects parameter 1 to be mysqli, resource given in F:\xampp\htdocs\RIA\insert.php on line 19

 

I tried googling the error but still not sure how to fix it.

Link to comment
https://forums.phpfreaks.com/topic/224543-mysql-insert-error/#findComment-1161540
Share on other sites

Define: change the mysqli to mysql?

 

You cannot just change the name because the parameter list is different. If the error message is exactly the same, then you didn't save the change or you didn't put the new file onto your server.

Link to comment
https://forums.phpfreaks.com/topic/224543-mysql-insert-error/#findComment-1161549
Share on other sites

Well i saved the file into xxamps htdocs folder.

<?php 
if (isset($_POST['required'])) {
   include ('connection.php');
   $title = $_POST['title'];
   $cost = $_POST['cost'];
   $stock = $_POST['no_in_stock'];
   $insert = "INSERT INTO book (title, cost, no_in_stock) VALUES ('$title', '$cost', '$stock')";
   if ( !mysql_query($connect, $insert) ) {
      die( "<br>Query string: $insert<br>Failed with error: " . mysql_error($connect) );
   } else {
      if( mysql_affected_rows($connect) > 0 ) {
         $newrecord = "New record added";
      }
   }
}
?>

I just got rid of the i on mysqli to make it mysql not sure if that was right or not.

Link to comment
https://forums.phpfreaks.com/topic/224543-mysql-insert-error/#findComment-1161556
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.