Jump to content
Jazza

Why isn't my form inserting data into MySQL database?

Recommended Posts

Hi

 

I am having trouble inserting data into my MySQL database.

This is my PHP code

ini_set('display_errors', 1);
	ini_set('display_startup_errors', 1);
	error_reporting(E_ALL);
static $connection;
	if(!isset($connection)) {
		$connection = new mysqli("localhost","username","password");
	}
	if ($connection->connect_error) {
	    die("Connection failed: " . $connection->connect_error);
	}
	
	$stmt = $connection->prepare("INSERT into Product(product_title,product_price,product_availability,productImage_1,productImage_2,productImage_3,productImage_4,product_description,product_shipping,product_pickup) VALUES(?,?,?,?,?,?,?,?,?,?)");
	$product_title = $_POST['title'];
	$product_price = $_POST['price'];
	$product_availability = $_POST['stock'];

	$null = NULL;

	$product_description = $_POST['description'];
	$product_shipping = $_POST['postage'];
	$product_pickup = $_POST['pickup'];
	
	$stmt->bind_param('sisbbbbsis',$product_title,$product_price,$product_availability,$null,$null,$null,$null,$product_description,$product_shipping,$product_pickup);
	
	$stmt->send_long_data(3, file_get_contents($_FILES['img1']['tmp_name']));
	$stmt->send_long_data(4, file_get_contents($_FILES['img2']['tmp_name']));
	$stmt->send_long_data(5, file_get_contents($_FILES['img3']['tmp_name']));
	$stmt->send_long_data(6, file_get_contents($_FILES['img4']['tmp_name']));
	
	$stmt->execute();
	
	$stmt->close();
	$connection->close();
	
	echo "Product inserted successfully";

I am also getting these errors.

Notice: Undefined index: title in /Applications/MAMP/htdocs/Bourke/insertproduct.php on line 61

Notice: Undefined index: price in /Applications/MAMP/htdocs/Bourke/insertproduct.php on line 62

Notice: Undefined index: stock in /Applications/MAMP/htdocs/Bourke/insertproduct.php on line 63

Notice: Undefined index: description in /Applications/MAMP/htdocs/Bourke/insertproduct.php on line 67

Notice: Undefined index: postage in /Applications/MAMP/htdocs/Bourke/insertproduct.php on line 68

Notice: Undefined index: pickup in /Applications/MAMP/htdocs/Bourke/insertproduct.php on line 69

Fatal error: Call to a member function bind_param() on boolean in /Applications/MAMP/htdocs/Bourke/insertproduct.php on line 71

Thanks in advance.

Edited by Jazza

Share this post


Link to post
Share on other sites

The $_POST index names 'title', 'price', 'stock' etc do not exist at all, so referencing them give you undefined error and so no values will be retrieved.

Share this post


Link to post
Share on other sites

The $_POST index names 'title', 'price', 'stock' etc do not exist at all, so referencing them give you undefined error and so no values will be retrieved.

Hi, thanks for the reply.

 

I forgot to add the HTML form.

<html>
	<head>


	</head>


	<body>
		<form action="insertproduct.php" method="POST" enctype="multipart/form-data">
			Title<input type="text" name="title"><br><br>
			Product Price<input type="number" name="price"><br><br>
			Stock remaining
			<select name="stock" id="stock">
				<option value="in stock">In stock</option>
				<option value="almost out">Almost out</option>
				<option value="out of stock">Out of stock</option>
			</select><br><br>
			Select image 1: <input type="file" name="img1" multiple><br><br>
			Select image 2: <input type="file" name="img2" multiple><br><br>
			Select image 3: <input type="file" name="img3" multiple><br><br>
			Select image 4: <input type="file" name="img4" multiple><br><br>
			<hr>
			Product Description:<br><br><textarea rows="10" cols="100" name="description">
			</textarea><br><br>
			<hr>
			Shipping Price($)<input type="number" name="postage"><br><br>
			Instore Pickup available? 
				<input type="radio" name="pickup" value="yes" id="yes" checked> Yes
				<input type="radio" name="pickup" value="no" id="no"> No
				<br><br>
				
			<input type="submit" value="submit">
		</form>
	</body>
</html>

Share this post


Link to post
Share on other sites

The original post still represent what will happen if $_POST index names do not exist, that you are referencing. This will happen if you go directly to the php page, without going from the form html page, the form is not submitted so $_POST Indexes and values do not exist. You should first check if these indexs are set (isset()) and assign these values to a predefined variable, then no such error will appear.

  • Like 1

Share this post


Link to post
Share on other sites

Hi

Thanks again for the reply.

 

So if I use the form, instead of just entering the php code script in the browser, those errors will not be displayed?

 

The data is not going into the database though.

 

Thanks

Share this post


Link to post
Share on other sites

Without static and if condition try it with just

 

$connection = new mysqli("localhost","username","password");

 

What do you mean without static and if condition?

 

Thanks

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...

×
×
  • Create New...