Jump to content

Error at these lines if($_GET['action']=='add'), if($_GET['action']=='exists') and $stmt = $con->prepare( $query );


Go to solution Solved by denno020,

Recommended Posts


can anyone please tell me what is wrong in this piece of code?

am having error where i mentioned in the title...

<?php
if($_GET['action']=='add'){
echo "<div>" . $_GET['prod_name'] . " was added to your favourites.</div>";
}

if($_GET['action']=='exists'){
echo "<div>" . $_GET['prod_name'] . " already exists in your favourites.</div>";
}

require "db_connect.php";

$query = "SELECT prod_id, prod_name, prod_price FROM tbl_product";
$stmt = $con->prepare( $query );
$stmt->execute();

$num = $stmt->rowCount();

 

You need to start telling us what the errors are! If it's what I think, then it's really the same answer as your last question, so read this and understand it:

 

If you try to access an element of an array and that element doesn't exist, you will get a notice from PHP (not truly an error). The way to prevent this is to check if the element of the array exists by calling isset($array['index']) before you try to access the element. Just like I wrote in the last question about your SESSION variable.

Edit: Appears boompa beat me to the submit button :).

 

Well for some proper help you'll need to share your error as well, otherwise we've got nothing to go on.. But I will have a guess that you're getting an error saying that $_GET['action'] isn't set?

 

You need to do it like this:

if(isset($_GET['action']) && $_GET['action'] == 'add'){
....
}

As for the other part, I'm not sure from looking at it what the error might be, let us know and I'm sure we'll be able to help more.

 

Denno

Edited by denno020

thank you for the help denno...it solved out part of my issue

 

am still having the following error - fatal error: call to a member function prepare() on a non-object

 

am having it due to the line 

$stmt = $con->prepare( $query ); 

can you tell me why please?

here it is 

$con = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);

is forms part of my old database connection file

 

my new database connection file does not have it

 

how can i proceed now...

I find it a bit weird that you're using PDO in your 'old' database connection and mysql_connect() in your new one, because mysql_connect has been deprecated..

 

I suggest you move back to using PDO.

 

And you will need to make sure you actually have a correct PDO object in the $con variable, so var_dump() it directly after initializing it, and see what it is

Well if you want to use mysql_connect, which again, I suggest you dont, but if you do, then that will create a connection to the database. You will then use that in another function, mysql_query(), which will take a string as the first paramater, which is the query, and the second parameter is your $con variable (which represents the connection to the database).

 

But seriously, you should learn to use PDO. It's really not hard, and will mean your code won't become useless when these functions are eventually removed from php..

ok thanks....i know my friend i should learn that...but am nearly at the end of my project and a bit too late for that.

 

i changed the code to this

$query ="SELECT prod_id, prod_name, prod_price FROM tbl_product";

$result = $mysql_query( $query );

but i need to alter this piece as well

$num = $stmt->rowCount();

how do i write this one?

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.