I am getting this error when I run the below code. I dont know why I am getting this error. I echoed the query to make sure that the syntax is correct I checked the query in a syntax checker to make sure that the query contains no syntax errors. and even tested in PHP my admin.
The query that is producing this error is the query that is in the for loop with numberofcolor and numberofsizes
Error description: 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 '1' at line 1
<?php include('includes/db.php'); include('../functions/functions.php');?>
<?php require './p_struct/head.php';?>
<?php require './p_struct/header.php';?>
<?php require './p_struct/sidenav.php';?>
<div class="main" style="font-size: 20px">
<form action="insert_product.php" method="post" enctype="multipart/form-data" class="form-horizontal" id="chells">
<table>
<tr>
<td>Name</td>
<td><input type="text" name="product_title" required="required"></td>
</tr>
<tr>
<td>category</td>
<td><select name="product_category" >
<option>Select a Category</option>
<?php
$get_cats = " select * from categories";
$run_cats = mysqli_query($con, $get_cats);
while($row_cats = mysqli_fetch_array($run_cats))
{
$cat_id = $row_cats['cat_id'];
$cat_title = $row_cats['cat_data'];
echo "
<option vallue = '$cat_id' >$cat_title </option>
";
}
?>
</select></td>
</tr>
<tr>
<td>featured</td>
<td><select name="product_featured" >
<option>featured</option>
<option>not featured</option>
</select></td>
</tr>
<tr>
<td>price</td>
<td><input type="text" name="product_price" required="required"></td>
</tr>
<tr>
<td>image</td>
<td><input type="file" name="product_image" required="required"></td>
</tr>
<tr>
<td>big image</td>
<td><input type="file" name="product_big_image" required="required"></td>
</tr>
<tr>
<td>cart image</td>
<td><input type="file" name="product_cart_image" required="required"></td>
</tr>
<tr>
<td>keywords</td>
<td><input type="text" name="product_keywords" required="required"></td>
</tr>
<tr>
<td> Colors</td>
<td>
<div class="checkbox">
<?php
$get_colors = "SELECT * FROM `product_colors`";
$run_get_colors = mysqli_query($con, $get_colors);
while($row_get_color = mysqli_fetch_array($run_get_colors))
{
$colorS = $row_get_color['color'];
echo '<label class="checkbox-inline" ><input type="checkbox" name = "mycolor[]" value="'.$colorS.'" >'.$colorS.'</label>';
}
?>
</div>
</td>
</tr>
<tr>
<td> Sizes</td>
<td>
<div class="checkbox">
<?php
$get_sizes = "SELECT * FROM `product_sizes`";
$run_get_sizes = mysqli_query($con, $get_sizes);
while($row_get_sizes = mysqli_fetch_array($run_get_sizes))
{
$sizesS = $row_get_sizes['size'];
echo '<label class="checkbox-inline" ><input type="checkbox" name = "mysizes[]" value="'.$sizesS.'" >'.$sizesS.'</label>';
}
?>
</div>
</td>
</tr>
<tr>
<td>description</td>
<td><textarea name="product_description" cols="20" rows="5" ></textarea></td>
</tr>
<tr>
<td><input type="submit" value="Add Product" name="insert_post"></td>
</tr>
</table>
</form>
</body>
</html>
<?php
global $con;
if( isset($_POST['insert_post']))
{
$a = rand(100, 999);
$b = rand(100, 999);
$product_id = $a.$b;
$product_title = $_POST['product_title'];
$product_category = $_POST['product_category'];
$product_featured = $_POST['product_featured'];
$product_price = $_POST['product_price'];
$product_keywords = $_POST['product_keywords'];
$product_description = $_POST['product_description'];
$color_array = $_POST['mycolor'];
$sizes_array = $_POST['mysizes'];
$product_image = $_FILES['product_image']['name'];
$product_big_image = $_FILES['product_big_image']['name'];
$product_cart_image = $_FILES['product_cart_image']['name'];
$product_image_tmp = $_FILES['product_image']['tmp_name'];
$product_big_image_tmp = $_FILES['product_big_image']['tmp_name'];
$product_cart_image_tmp = $_FILES['product_cart_image']['tmp_name'];
move_uploaded_file($product_image_tmp,"product_images/$product_image");
move_uploaded_file($product__big_image_tmp,"product_big_images/$product_big_image");
move_uploaded_file($product_cart__image_tmp,"product_cart_images/$product_cart_image");
$insert_product = " insert into products (product_id,product_cat, product_featured, product_title, product_price, product_desc,product_image,product_big_image,product_cart_image,product_keywords) values ($product_id,'$product_category','$product_featured','$product_title','$product_price','$product_description','$product_image','$product_big_image','$product_cart_image','$product_keywords')";
$run_insert_query = mysqli_query($con, $insert_product);
$number_of_colors = count($color_array);
for($i=0; $i <$number_of_colors; $i++)
{
$curr_color_value = $color_array[$i];
$add_color_att = "INSERT INTO `attributes`( `id`, `att_type`, `att_value`) VALUES ($product_id, 'color','$curr_color_value');";
$run_add_color_att = mysqli_query($con, $add_color_att);
/* if($run_add_color_att)
{
echo "COLOR ADDED!";
}
else {
echo "COLOR NOT ADDED!";
}*/
}
$number_of_sizes = count($sizes_array);
for($s=0; $s <$number_of_sizes; $s++)
{
$curr_size_value = $number_of_sizes[$s];
$add_size_att = "INSERT INTO `attributes`( `id`, `att_type`, `att_value`) VALUES ($product_id, `size`,`$curr_size_value`)";
$run_add_size_att = mysqli_query($con, $add_size_att);
/* if($run_add_size_att)
{
echo "SIZE ADDED!";
}
else {
echo "SIZE NOT ADDED!";
}*/
}
if($run_insert_query)
{
echo "PRODUCT ADDED!";
}
else {
echo "PRODUCT NOT ADDED!";
}
} ?>
</div>
$add_size_att = "INSERT INTO `attributes`( `id`, `att_type`, `att_value`) VALUES ($product_id, `size`,`$curr_size_value`)";
your using the incorrect quote type here the ` should be ' around the string values:
$add_size_att = "INSERT INTO `attributes`( `id`, `att_type`, `att_value`) VALUES ($product_id, 'size','$curr_size_value')";
also should size be $size?
Related
when i run the code it give error of object of class mysqli_connect could not be converted to string in line 135. please some one provide me the solution of this problem.
i tried searching for solution but no one worked for me. bellow is the code showing error.
$row = mysqli_connect("$con,$insert_product");
accepted to run but showing error in line 135. what mistake do i have make.full code is here
<?php require_once ("includes/db.php");
?>
<!DOCTYPE html>
<html>
<head>
<title> product</title>
</head>
<body>
<form method="post" action="insert_product.php" enctype="multipart/form-data">
<table width="700" align="center">
<tr>
<td><h2>insert new product</h2></td>
</tr>
<tr>
<td>Product title</td>
<td><input type="text" name="product_title"></td>
</tr>
<tr>
<td>Product category</td>
<td><select name="product_cat">
<option>Select a category</option>
<?php
$get_cats = "select * from category";
$run_cats= mysqli_query($con,$get_cats);
while ($row_cats=mysqli_fetch_array($run_cats))
{
$cat_id = $row_cats['cat_id'];
$cat_title=$row_cats['cat_title'];
echo "<option value='$cat_id'>$cat_title</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>Product brand</td>
<td><select name="product_brand">
<option>Select brand</option>
<?php
$get_brand = "select * from brand";
$run_brand= mysqli_query($con,$get_cats);
while ($row_brand=mysqli_fetch_array($run_brand))
{
$brand_id = $row_brand['brand_id'];
$brand_title=$row_brand['brand_title'];
echo "<option value='$cat_id'>$cat_title</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>product image1</td>
<td><input type="file" name="product_img1"></td>
</tr>
<tr>
<td>Product image2</td>
<td><input type="file" name="product_img2"></td>
</tr>
<tr>
<td>Product image3</td>
<td><input type="file" name="product_img3"></td>
</tr>
<
<tr>
<td>Product price</td>
<td><input type="number" name="product_price"></td>
</tr>
<tr>
<td>Product desceptration</td>
<td><input type="text" name="product_desc"></td>
</tr>
<tr>
<td>Product keyword</td>
<td><input type="number" name="product_keyword"></td>
</tr>
<tr>
<td><input type="submit" value="submit" name="submit"></td>
</tr>
</table>
</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
$product_title=$_POST['product_title'];
$product_cat=$_POST['product_cat'];
$product_brand=$_POST['product_brand'];
$product_price=$_POST['product_price'];
$product_desc=$_POST['product_desc'];
$status='on';
$product_keyword=$_POST['product_keyword'];
//image name
$product_img1 = $_FILES['product_img1']['name'];
$product_img2 = $_FILES['product_img2']['name'];
$product_img3 = $_FILES['product_img3']['name'];
//image temp names
$temp_name1 = $_FILES['product_img1']['tmp_name'];
$temp_name2 = $_FILES['product_img2']['tmp_name'];
$temp_name3 = $_FILES['product_img3']['tmp_name'];
if($product_title=='' OR $product_cat=='' OR $product_brand=='' OR $product_price=='' OR $product_desc=='' OR $product_keyword=='' OR $product_img1=='' OR $product_img2=='' OR $product_img3=='')
{
echo "<script> alert ('please insert the data in the form')</script>";
exit();
}
else{
//uploadinimage to the folder
move_uploaded_file($temp_name1,"product_images/$product_img1");
move_uploaded_file($temp_name2, "product_images/$product_img2");
move_uploaded_file($temp_name3,"product_images/$product_img3");
}
$insert_product = "insert into products (cat_id,brand_id,date,product_title,product_img1,product_img2,product_img3,product_brand,product_price,product_desc,status) values ('$product_cat','$product_brand',NOW(),'$product_title','$product_img1','$product_img2',$product_img3,'product_brand','product_price','product_desc','product_keyword')";
$row = mysqli_connect("$con,$insert_product");
if($row)
{
echo "<script> alert('insert sucessfully')</script>";
}
else
{
echo "<script> alert(' unsucessfull to insert')</script>";
}
}
?>
I don't think you're meaning to connect again. Rather, it appears you may be trying to perform a query?
Your code:
$insert_product = "insert into products (cat_id,brand_id,date,product_title,product_img1,product_img2,product_img3,product_brand,product_price,product_desc,status) values ('$product_cat','$product_brand',NOW(),'$product_title','$product_img1','$product_img2',$product_img3,'product_brand','product_price','product_desc','product_keyword')";
$row = mysqli_connect("$con,$insert_product");
Not only are you referencing a variable never defined in the script (though it probably is in the file you require at the top), but you're passing a query right to mysqli_connect(). I imagine what you actually meant to do is:
$insert_product = "insert into products (cat_id,brand_id,date,product_title,product_img1,product_img2,product_img3,product_brand,product_price,product_desc,status) values ('$product_cat','$product_brand',NOW(),'$product_title','$product_img1','$product_img2',$product_img3,'product_brand','product_price','product_desc','product_keyword')";
$row = mysqli_query($con, $insert_product);
EDITED I am using HTML forms and PHP to upload data to a MySQL database. When I submit the information an image that i uploaded gets moved to a specific folder(the way i intended it to), but the data does not get uploaded to the data base.
I added most of the code now, the site didn't allow me to add everything, I deleted some lines that in my opinion don't have anything to do with the problem at hand. so maybe you'll see the problem:
<!DOCTYPE>
<?php
include("includes/db.php");
?>
<html>
<head>
<title>Inserting Product</title>
<script src="//tinymce.cachefly.net/4.1/tinymce.min.js"></script>
<script>
tinymce.init({selector:'textarea'});
</script>
</head>
<body bgcolor="grey">
<form action="insert_product.php" method="post" enctype="multipart/form-
data">
<table align="center" width="795" border="2" bgcolor="lime">
</tr>
<tr>
<td align="right"><b>Product Category:</b></td>
<td>
<select name="product_cat" >
<option>Select a Category</option>
<?php
$get_cats = "select * from categories";
$run_cats = mysqli_query($con, $get_cats);
while ($row_cats=mysqli_fetch_array($run_cats)){
$cat_id = $row_cats['cat_id'];
$cat_title = $row_cats['cat_title'];
echo "<option value='$cat_id'>$cat_title</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td align="right"><b>Product Publisher:</b></td>
<td> <select name="product_dev">
<option>Select a Publisher</option>
<?php
$get_devs = "select * from developers";
$run_devs = mysqli_query($con, $get_devs);
while ($row_devs=mysqli_fetch_array($run_devs)){
$dev_id = $row_devs['dev_id'];
$dev_title = $row_devs['dev_title'];
echo "<option value='$dev_id'>$dev_title</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td align="right"><b>Product Image:</b></td>
<td><input type="file" name="product_image" /></td>
</tr>
<tr>
<td align="right"><b>Product Price:</b></td>
<td><input type="text" name="product_price" required/></td>
</tr>
<tr>
<td align="right"><b>Product Keywords:</b></td>
<td><input type="text" name="product_keywords" size="50"
required/></td>
</tr>
<tr align="center">
<td colspan="7"><input type="submit" name="insert_post"
value="Insert Product Now"/></td>
</tr>
</table>
</form>
</body>
</html>
<?php
if(isset($_POST['insert_post'])){
$product_title = $_POST['product_title'];
$product_cat= $_POST['product_cat'];
$product_pub = $_POST['product_pub'];
$product_dev = $_POST['product_dev'];
$product_price = $_POST['product_price'];
$product_desc = $_POST['product_desc'];
$product_keywords = $_POST['product_keywords'];
$product_image = $_FILES['product_image']['name'];
$product_image_tmp = $_FILES['product_image']['tmp_name'];
move_uploaded_file($product_image_tmp,"product_images/$product_image");
$insert_product = "insert into products
(product_cat,product_pub,product_dev,
product_title,product_price,product_desc,product_image,
product_keywords) values ('$product_cat','$product_pub','$product_dev',
'$product_title','$product_price','$product_desc',
'$product_image','$product_keywords')";
$insert_pro = mysqli_query($con, $insert_product);
if($insert_pro){
echo "<script>alert('Product Has been inserted!')</script>";
echo "<script>window.open('index.php?insert_product','_self')
</script>";
}
}
?>
Your query seems fine.
Just verify your MySQLi connection.
It should be like:
$con = mysqli_connect("HOST", "USERNAME", "PASSWORD", "DATABASE_NAME");
I am new in php. when form is submitted the data is not storing in database not getting what is wrong in code. Below is my code
<form method="post" action="insert_product.php" enctype="multipart/form-data">
<table width="700" align="center" >
<tr>
<td><h1>Insert New Product</h1></td>
</tr>
<tr>
<td><b>Product Title</b></td>
<td><input type="text" name="product_title"></td>
</tr>
<tr>
<td><b>Product Category</b></td>
<td>
<select name="product_cat">
<option>Select Category</option>
<?php
$get_cats = "select * from categories";
$run_cats = mysqli_query($con , $get_cats);
while ( $row_cats = mysqli_fetch_array($run_cats)) {
$cat_id = $row_cats['cat_id'];
$cat_title = $row_cats['cat_title'];
echo "<option value='$cat_id'>$cat_title</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td><b>Product Brand</b></td>
<td>
<select name="product_brand">
<option>Select Brand</option>
<?php
$get_brands = "select * from brands";
$run_brands = mysqli_query($con , $get_brands);
while ($row_brands = mysqli_fetch_array($run_brands)) {
$brand_id = $row_brands['brand_id'];
$brand_title = $row_brands['brand_title'];
echo "<option value='$brand_id'>$brand_title</option>";
}
?>
</select>
</td>
</tr>
<tr> <td><b>Product Image1</b></td>
<td><input type="file" name="product_img1"></td>
</tr>
<tr><td><b>Product Image2</b></td>
<td><input type="file" name="product_img2"></td>
</tr>
<tr><td><b>Product Image3</b></td>
<td><input type="file" name="product_img3"></td>
</tr>
<tr>
<td><b>Product Price</b></td>
<td><input type="text" name="product_price"></td>
</tr>
<tr>
<td><b>Product Info</b></td>
<td> <textarea name="product_desc">Easy! You should check out MoxieManager!</textarea></td>
</tr>
<tr>
<td><b>Product Keyword</b></td>
<td><input type="text" name="product_keyword"></td>
</tr>
<tr>
<td><input type="submit" name="insert_product" value="Insert"></td>
</tr>
</table>
</form>
and php code of it
<?php
if (isset($_POST['insert_product'])) {
$product_title = $_POST['product_title'];
$product_cat = $_POST['product_cat'];
$product_brand = $_POST['product_brand'];
$product_price = $_POST['product_price'];
$product_desc = $_POST['product_desc'];
$status = 'on';
$product_keyword = $_POST['product_keyword'];
//image names//
$product_img1 = $_FILES['product_img1']['name'];
$product_img2 = $_FILES['product_img2']['name'];
$product_img3 = $_FILES['product_img3']['name'];
//temp image names
$temp_name1 = $_FILES['product_img1']['tmp_name'];
$temp_name2 = $_FILES['product_img2']['tmp_name'];
$temp_name3 = $_FILES['product_img3']['tmp_name'];
if ($product_title=='' OR $product_cat=='' OR $product_brand=='' OR $product_price=='' OR $product_desc=='' OR $product_keyword=='' OR $product_img1=='' ) {
echo "<script>alert('Please Fill Everything')</script>";
exit();
}
else {
//uploading images to its folder
move_uploaded_file($temp_name1, "product_images/$product_img1");
move_uploaded_file($temp_name2, "product_images/$product_img2");
move_uploaded_file($temp_name3, "product_images/$product_img3");
$insert_product = "insert into products (cat_id,brand_id,date,product_title,product_img1,product_img2,product_img3,product_price,product_desc,status)
values ('$product_cat','$product_brand',NOW(),'$product_title','$product_img1','$product_img2','$product_img3','$product_price','$product_desc','$status')";
$run_product = mysqli_query($con, $insert_product);
if ($run_product) {
echo "<script>alert('Product Inserted')</script>";
}
}
}
?>
Till alert(fill everything) is working means when i click submit without filling form the alert is coming but after that when i filled everything and submit it. But alert is not coming and also data is not saving in database. Please help me. adding my database structure also. Thanks
i am a beginner and am trying to make a form validation with PHP. i want to check if one of the inputs is empty, the form says that the empty input is required.
I show the php script behind.
<?php
$titleErr = $authorErr = $keywordsErr = $contentErr = "";
$title = $author = $keywords = $content = "";
if (empty($_POST["submit"])) {
if(empty($_POST["title"])){
$titleErr = "title is required";
}
if(empty($_POST["author"])){
$authorErr = "author name is required";
}
if(empty($_POST["keywords"])){
$keywordsErr = "keywords are required";
}
if(empty($_POST["content"])){
$contentErr = "This field is required";
}
}
?>
<form method="post" action="insert_post.php">
<table width="600" align="center" border="10">
<tr>
<td align="center" bgcolor="yellow" colspan="6"><h1>Insert New Post Here</h1></td>
</tr>
<tr>
<td align="right">Post Title:</td>
<td><input type="text" name="title" size="38">
<span style="color:red;"><?php echo $titleErr; ?></span>
</td>
</tr>
<tr>
<td align="right">Post Author:</td>
<td><input type="text" name="author" size="38">
<span style="color:red;"><?php echo $authorErr; ?></span>
</td>
</tr>
<tr>
<td align="right">Post Keywords:</td>
<td><input type="text" name="keywords" size="38">
<span style="color:red;"><?php echo $keywordsErr; ?></span>
</td>
</tr>
<tr>
<td align="right">Post Image:</td>
<td><input type="file" name="image"></td>
</tr>
<tr>
<td align="right">Post Content</td>
<td><textarea name="content" cols="30" rows="15"></textarea>
<span style="color:red;"><?php echo $contentErr; ?></span>
</td>
</tr>
<tr>
<td align="center" colspan="6"><input type="submit" name="submit" value="Publish Now"></td>
</tr>
</table>
</form>
</body>
</html>
<?php
include("includes/connect.php");
if(isset($_POST['submit'])){
$title = $_POST['title'];
$date = date('d-m-Y');
$author = $_POST['author'];
$keywords = $_POST['keywords'];
$content = $_POST['content'];
$image = $_FILES['image'] ['name'];
$image_tmp = $_FILES['image'] ['tmp_name'];
move_uploaded_file($image_tmp, "../images/$image");
$query = "INSERT INTO posts (post_title, post_date, post_author, post_keywords, post_image, post_content) VALUES('$title', '$date', '$author', '$keywords', '$image', '$content')";
$result = mysqli_query($conn, $query);
if($query){
echo"<center><h1>Post Published Succesfully!</h1></center>";
}
}
?>
The problem is i want to stop the scrit if the inputs are empty but i can't use functions like: exit() and break;.
and if i submit, the form sends empty values to the database :C.
how can i solve this problem?
We make use of session variables which temporarily stores data locally. To use sessions we must always start sessions by session_start() at the start of every page to be able to access the variables. Now we can store data and pass it to whatever page we want to without having to send it - we just store it and then grab it.
index.php
<?php
session_start();
$titleErr = $authorErr = $keywordsErr = $contentErr = "";
$title = $author = $keywords = $content = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(empty($_POST["title"])){
$titleErr = "title is required";
}
if(empty($_POST["author"])){
$authorErr = "author name is required";
}
if(empty($_POST["keywords"])){
$keywordsErr = "keywords are required";
}
if(empty($_POST["content"])){
$contentErr = "This field is required";
}
if(!empty($_POST["title"]) && !empty($_POST["author"]) && !empty($_POST["keywords"]) && !empty($_POST["content"])){
$_SESSION["title"] = $_POST["title"];
$_SESSION["author"] = $_POST["author"];
$_SESSION["keywords"] = $_POST["keywords"];
$_SESSION["content"] = $_POST["content"];
$_SESSION["image"] = $_FILES["image"];
header("location: insert_post.php");
exit();
}
}
?>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table width="600" align="center" border="10">
<tr>
<td align="center" bgcolor="yellow" colspan="6"><h1>Insert New Post Here</h1></td>
</tr>
<tr>
<td align="right">Post Title:</td>
<td><input type="text" name="title" size="38" value="<?php if(isset($_POST['title'])){ echo $_POST['title']; }; ?>">
<span style="color:red;"><?php echo $titleErr; ?></span>
</td>
</tr>
<tr>
<td align="right">Post Author:</td>
<td><input type="text" name="author" size="38" value="<?php if(isset($_POST['author'])){echo $_POST['author']; }; ?>">
<span style="color:red;"><?php echo $authorErr; ?></span>
</td>
</tr>
<tr>
<td align="right">Post Keywords:</td>
<td><input type="text" name="keywords" size="38" value="<?php if(isset($_POST['keywords'])){echo $_POST['keywords']; }; ?>">
<span style="color:red;"><?php echo $keywordsErr; ?></span>
</td>
</tr>
<tr>
<td align="right">Post Image:</td>
<td><input type="file" name="image"></td>
</tr>
<tr>
<td align="right">Post Content</td>
<td><textarea name="content" cols="30" rows="15" value="<?php if(isset($_POST['content'])){echo $_POST['content']; }; ?>"></textarea>
<span style="color:red;"><?php echo $contentErr; ?></span>
</td>
</tr>
<tr>
<td align="center" colspan="6"><input type="submit" name="submit" value="Publish Now"></td>
</tr>
</table>
</form>
</body>
</html>
insert_post.php
<?php
session_start();
include("includes/connect.php");
$title = $_SESSION['title'];
$author = $_SESSION['author'];
$keywords = $_SESSION['keywords'];
$content = $_SESSION['content'];
$date = date('d-m-Y');
$image = $_SESSION['image']['name'];
$image_tmp = $_SESSION['image']['tmp_name'];
move_uploaded_file($image_tmp, "../images/$image");
$query = "INSERT INTO posts (post_title, post_date, post_author, post_keywords, post_image, post_content) VALUES('$title', '$date', '$author', '$keywords', '$image', '$content')";
$result = mysqli_query($conn, $query);
if($query){
echo"<center><h1>Post Published Succesfully!</h1></center>";
}
else{
echo "<center><h1>Error! Post was not published!</h1></center>";
}
echo $title . " " . $author . " " . $keywords . " " . $content;
?>
Btw, you should be using prepared statements for your database inserts to prevent SQL-injections. Read more about it here.
You don't hAve to Break the Script using the Exit Funktion. Just prove if the error variables aren't empty and only in the Case when they aren't empty send a dB-request.
Chenge these lines:
include("includes/connect.php");
if(isset($_POST['submit'])){
to
include("includes/connect.php");
if( isset($_POST['submit']) //Fix here, it will only insert if all error message
&& empty($titleErr) //are empty.
&& empty($authorErr)
&& empty($keywordsErr)
&& empty($contentErr) ){
Edit
And also change this line
if (empty($_POST["submit"])) {
To
if (isset($_POST["submit"])) {
On the init of your script. The empty function will return true if you did not submit the page, that's why you are hving the error messages.
I've got a page showing the contents of my DB in form inputboxes like this:
<?php
while($row = mysql_fetch_array($result))
{
$namn = $row['namn'];
$mandag = $row['mandag'];
$tisdag = $row['tisdag'];
$onsdag = $row['onsdag'];
$torsdag = $row['torsdag'];
$fredag = $row['fredag'];
?>
<td width="100"></td>
<td><?=$namn?><input name="namn[]" type="hidden" value="<?=$namn?>"></td>
</tr>
<tr>
<td width="100">Mandag</td>
<td><input name="mandag[]" type="text" value="<?=$mandag?>"></td>
</tr>
<tr>
<td width="100">Tisdag</td>
<td><input name="tisdag[]" type="text" value="<?=$tisdag?>"></td>
</tr>
<tr>
<td width="100">Onsdag</td>
<td><input name="onsdag[]" type="text" value="<?=$onsdag?>"></td>
</tr>
<tr>
<td width="100">Torsdag</td>
<td><input name="torsdag[]" type="text" value="<?=$torsdag?>"></td>
</tr>
<tr>
<td width="100">Fredag</td>
<td><input name="fredag[]" type="text" value="<?=$fredag?>"></td>
</tr>
<?php } ?>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="update" type="submit" id="update" value="Update">
</td>
</tr>
</table>
</form>
After this I've added code to able to update the different DB entries by changing the content of the inputboxes and pressing the submit button:
<?php
if(isset($_POST['update']))
{
$namnValue = $_POST['namn'];
$mandagValue = $_POST['mandag'];
$tisdagValue = $_POST['tisdag'];
$onsdagValue = $_POST['onsdag'];
$torsdagValue = $_POST['torsdag'];
$fredagValue = $_POST['fredag'];
print_r($mandagValue);
$sql = "UPDATE anstalld SET mandag = '$mandagValue', tisdag = '$tisdagValue', onsdag = '$onsdagValue', torsdag = '$torsdagValue', fredag = '$fredagValue' WHERE namn = '$namnValue'";
echo $sql;
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
}
mysql_close($conn);
?>
The DB is being updated, however, the problem is that all my
$namnValue = $_POST['namn'];
$mandagValue = $_POST['mandag'];
$tisdagValue = $_POST['tisdag'];
$onsdagValue = $_POST['onsdag'];
$torsdagValue = $_POST['torsdag'];
$fredagValue = $_POST['fredag'];
are returning the result "Array", an not the actual Values from the inputboxes.
Therefore my SQL UPDATE ends up being
"UPDATE anstalld SET mandag = 'Array', tisdag = 'Array', onsdag =
'Array', torsdag = 'Array', fredag = 'Array' WHERE namn = 'Array'"
I'll appreciate any help I can get on this, thanks.
You need to delete [] on our input names:
<td><input name="onsdag" type="text" value="<?=$onsdag?>"></td>
instead of
<td><input name="onsdag[]" type="text" value="<?=$onsdag?>"></td>
^^
Otherwise they are considered as arrays.
Because of the name of your input fields
<input name="onsdag[]" type="text" value="<?=$onsdag?>">
you are sending arrays and not single values.
Change the names as the previous answer suggests
<input name="onsdag" type="text" value="<?=$onsdag?>">
or access them as arrays
$namnValue = $_POST['namn'][0];
$mandagValue = $_POST['mandag'][0];
...