Database not inserting why?


#1

hi there, i wonder if you can help. i am trying to follow a tutorial and i have followed it exactly but when it comes to inserting data into the database it is not doing so. it says it is connected. here are my files. many thanks for the help.

bodyleft.php

<div id="bodyleft"><h3>Content Mangerment System</h3>
    <ul>
    	<li><a href="index.php">Home</a></li>
        <h3>Category Management</h3>
        <li><a href="index.php?viewall_cat">Viewall Categories</a></li>
        <li><a href="index.php?viewall_sub_cat">Viewall Sub Categories</a></li>
        <h3>Product Management</h3>
        <li><a href="index.php?add_products">Add New Products</a></li>
        <li><a href="index.php?viewall_products">View All Products</a></li>
        <li><a href="index.php?dis_pro">View All Discount Products</a></li>
        <li><a href="index.php?wish">View All WishList Products</a></li>
        <li><a href="index.php?out_stock">View All Out Of Stock Products</a></li>
    	<h3>Order Management</h3>
        <li><a href="index.php?complete_order">View All Complete Orders</a></li>
    	<li><a href="index.php?pending_order">View All Pending Orders</a></li>
    	<li><a href="index.php?cancle_order">View All Cancle Orders</a></li>
        <h3>Other Management</h3>
        <li><a href="index.php?customer">View All Customers</a></li>
        <li><a href="index.php?country">View All Country</a></li>
        <li><a href="index.php?state">View All State</a></li>
        <li><a href="index.php?slider">Edit Image Slider</a></li>
    </ul>  
</div>

<?php
	if(isset($_GET['viewall_cat'])){
		include("cat.php");	
	}
?>

bodyright.php

<?php 
	if(!isset($_GET['viewall_cat'])){ ?>
<div id="bodyright"></div>
<?php }?>

cat.php

<div id="bodyright">
	<h3>View All Categories</h3>
    <form method="post">
    	<table cellspacing="0">
        	<tr>
            	<td>Enter Category Name :</td>
                <td><input type="text" name="cat_name" /></td>
            </tr>
        </table>
        <center><button name="add_cat">Add Category</button></center>
    </form>
</div>
<?php include("inc/dbtest.php");
if(isset($_POST['add_cat'])){
$cat_name=$_POST['cat_name'];
$add_cat=$con->prepare("insert into main_cat(cat_name)values('$cat_name')");
$add_cat->execute();
}
?>

dbtest.php

 <?php
$servername = "";
$username = "";
$password = "";

try {
    $conn = new PDO("mysql:host=$servername;dbname=bargaincentre1_store", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
?>

#2

You have a few things to fix.

If the tutorial is telling you to add variables into your queries, it is not showing you how to learn prepared statements.


#3

The tutorial should have also had you set php’s error_reporting to E_ALL, display_errors to ON, and output_buffering to OFF, in the php.ini on your system, so that php will help you by reporting and displaying all the errors it detects. Stop and start your web server to get any changes made to the php.ini to take effect. You have a typo mistake in a variable name that would be throwing a php error to alert you to the problem.


#4

thanks for your reply both of you. i am going to do some work on prepared statements. i have also add <?php error_reporting(E_ALL); ini_set(‘display_errors’, 1); to the top of the page after the php opens.


#5

ok i have tried to research prepare and found a tutorial from developphp.com
https://www.youtube.com/watch?v=9QSAOTrM9H4

i have tried to put the code all together and it is still not loading data to the database. could someone please tell me to tell me where i am going wrong please. i have tried to add javascript at the end to tell me if the data has been entered as well. data is not going to php my admin. here is my code now

dbtest.php

 <?php
$servername = "";
$username = "";
$password = "";

try {
    $conn = new PDO("mysql:host=$servername;dbname=bargaincentre1_store", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
?> 

bodyleft.php

<div id="bodyleft"><h3>Content Mangerment System</h3>
    <ul>
    	<li><a href="index.php">Home</a></li>
        <h3>Category Management</h3>
        <li><a href="index.php?viewall_cat">Viewall Categories</a></li>
        <li><a href="index.php?viewall_sub_cat">Viewall Sub Categories</a></li>
        <h3>Product Management</h3>
        <li><a href="index.php?add_products">Add New Products</a></li>
        <li><a href="index.php?viewall_products">View All Products</a></li>
        <li><a href="index.php?dis_pro">View All Discount Products</a></li>
        <li><a href="index.php?wish">View All WishList Products</a></li>
        <li><a href="index.php?out_stock">View All Out Of Stock Products</a></li>
    	<h3>Order Management</h3>
        <li><a href="index.php?complete_order">View All Complete Orders</a></li>
    	<li><a href="index.php?pending_order">View All Pending Orders</a></li>
    	<li><a href="index.php?cancle_order">View All Cancle Orders</a></li>
        <h3>Other Management</h3>
        <li><a href="index.php?customer">View All Customers</a></li>
        <li><a href="index.php?country">View All Country</a></li>
        <li><a href="index.php?state">View All State</a></li>
        <li><a href="index.php?slider">Edit Image Slider</a></li>
    </ul>  
</div>

<?php
	if(isset($_GET['viewall_cat'])){
		include("cat.php");	
	}
?>

bodyright.php

<?php 
	if(!isset($_GET['viewall_cat'])){ ?>
<div id="bodyright"></div>
<?php }?>

and here is cat.php where the problems are i think

<div id="bodyright">
	<h3>View All Categories</h3>
    <form method="post">
    	<table cellspacing="0">
        	<tr>
            	<td>Enter Category Name :</td>
                <td><input type="text" name="cat_name" /></td>
            </tr>
        </table>
        <center><button name="add_cat">Add Category</button></center>
    </form>
</div>
<?php 
include("inc/dbtest.php");
if(isset($_POST['add_cat'])){
$cat_name= $_POST['cat_name'];
$con =("insert into main_cat (cat_name) values ('$cat_name')");
$add_cat = $con->prepare($con);
$add_cat->blindParam(':cat_name', $cat_name);
$add_cat->execute();

if($add_cat->execute()){
echo "<script>alert('Category Added Successfully!!!');</script>";
}
else{
echo "<script>alert('Category Not Added Successfully!!!');</script>";
}
}
?>

i do thanks people for helping as i can learn coding more, thank you.

through the last bit of cat.php i thought i was calling the database file and then seeing if add_cat had been pressed. if it had been pressed i was then to post cat_name to the database by inserting into database main_cat, table cat_name. then prepare the statement by cleaning it from mysql injection by blindParam and then execute. then seeing if data had been sent up by the java tags at the end. where am i going wrong?


#6

What output do you get after you submit the form? You should be getting a php error of some type.


#7

I believe I already pointed out that this was an issue; and how to correct it.


#8

the data is just not going up to database and leaving me with the page to start again


#9

i know astonecipher the code before was worse than that so i rearranged it. the code before was like this

<?php include("inc/dbtest.php"); if(isset($_POST['add_cat'])){ $cat_name=$_POST['cat_name']; $add_cat=$con->prepare("insert into main_cat(cat_name)values('$cat_name')"); $add_cat->execute(); } ?>

i watch the tutorial and thought with what i wanted to do with it the code what i am trying would work but it hasnt. the code now is completely different.

you didnt tell me how to correct it. you said about prepared statements so i watched this tutorial

how came up with th9is code but it still doesnt work. i dont understand what i am doing wrong. sorry


#10

And once again…

This is how I said to correct it…


#11

do you mean like this?

<?php include("inc/dbtest.php");
if(isset($_POST['add_cat'])){
$cat_name=$_POST['cat_name'];
$add_cat=$con->prepare(“insert into main_cat(cat_name)values($cat_name)”);
$add_cat->execute([$_POST[‘cat_name’]]);
}
?>

this is doing my head in lol i have tried php a few times and i cant get the gist of it lol this is now coming up with a blank page and not even showing the page with the input.


#12

I asked what output you are getting. If you didn’t get a php error message below the form that starts with - Fatal error: Uncaught Error: Call to a member function prepare() on string in …, then you haven’t successfully set up the reporting and display of errors like I suggested.

Are you still trying to learn and develop code on a web host (I just reviewed your previous threads on this forum) instead of a localhost development system? If so, a lot of the cheap/free hosts disable numerous php functions and you may not be able to get php to display the errors it finds (errors may be available in the error log on your server.)

You should be learning, developing, and debugging your code/queries on a localhost development system on your computer.


#13

i put error_reporting(E_ALL); ini_set(‘display_errors’, 1); just after the php begining tag. yesterday when i put the error report in it was working and coming up with an error but i have just put it in now and it is coming up with no error at the bottom. i feel like giving up but i am determined to learn this php language if it kills me. i understand it when i watch a tutorial but not good at doing the code myself.


#14

First of all, I stated to put the settings in the php.ini, so that ALL php errors would get reported, you didn’t do that. The blank page you are currently getting is because you copied code from this forum and you now have curly/smart quotes in it that are producing php syntax errors. When there is a php syntax error, your code doesn’t run, so the lines of code trying to set the error_reporting/display_errors settings are never executed.

Putting the lines of code to set error_reporting/display_errors into your code will only show you run-time errors.

As to fixing the curly/smart-quotes that have now corrupted your source file. You will need to delete them and retype them using straight quotes.


#15

sorry i give up. this is about 100 times i have tried to learn php. it isnt me sorry may be i am to thick, i have had enough sorry


#16

Take a break, clear your head. Not a big deal. I have those times and I have been doing this AWHILE.


#17

These do not compare currently.


#18

thanks for the confidence astonecipher, your a top fella. i have tried many times. i have watch many tutorials from social networking to e commerce and understand it when someone else is telling me but when i try to put the code together myself everything goes wrong


#19

could you show me please astonecipher how you would write it please. perhaps i could learn off the way you write it. if you could you would get a christmas card for next year lol i would really appreciate it.

you have seen my pages, how would you write it?


#20

I see the problem. Before giving it away, take a look at this very good PDO tutorial. Pay special attention to the format for named parameters.

https://phpdelusions.net/pdo