Hi,

After two weeks of not touching my web codings. PHP just gave me tons of surprises. I'm confused. I don't know what happened but I have this page entering a new category:

<form name="new_category" action="savenewcategory.php" method="post">
    <input type="text" name="category" size="30"><input type="submit" value="Add">
</form>

savenewcategory.php

<?php
$con=mysql_connect("localhost","root");
mysql_select_db("cart0902",$con);

    $category=$_POST['category'];

    $sql="insert into tblcategory(category) values('".$category."')";
    mysql_query($sql);


    echo "<script>";
    echo " self.location='page_category.php';";
    echo "</script>"; 
?>

and got this when i hit add(on the form)

"; echo " self.location='page_category.php';"; echo ""; ?>

---
I don't know what happened, a week ago, my shopping cart is perfectly working..and now.. I got this error on almost all of the pages. I tried removing the three echos, I got a blank page BUT the category is not added on the database!

What to do! Please help me! Thank you!

your db connection doesnt contain a password

the code i am using doesn't contain a password. is it really necessary? why is having a password made that kind of output?

to connect to a database you need the following information:

HOST => Generally is localhost
USER => What ever it is set as (would suggest not using root unless it is development)
PASSWORD => The password that is linked with the user
DATABASE TABLE => The table from which you wish to obtain information from.

If you have no password in the connection, your connection will fail. Think of it as logging in to an email account without a password (:))

http://php.net/manual/en/function.mysql-connect.php

Print out your $sql and run it independently in PHPMyAdmin or other MySQL applications..

can you show us the errors????

The database connection has nothing to do with his problem. These kind of output errors are often caused by hidden unicode characters in the source script. If mysql_query would crash due to errors, the script stops and nothing after it would be outputted.

If you have no password in the connection, your connection will fail. Think of it as logging in to an email account without a password (:)) http://php.net/manual/en/function.mysql-connect.php

@Squidge: If you check the function definition in the link you posted, you will see that all parameters (including the password) are optional. So the connection will not necessarily fail.

so what am I going to do with this?

Member Avatar for diafol

Always use error code in development:

$link = mysql_connect('localhost', 'root', ''); //either leave off pw or =''
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo "Connected successfully<br />";
mysql_close($link);
$db_selected = mysql_select_db('cart0902', $link);
if (!$db_selected) {
    die ('Can\'t use database : ' . mysql_error());
}
echo "db selected fine<br />";


$category = mysql_real_escape_string($_POST['category']) //CLEAN INPUT!!

$sql="INSERT INTO tblcategory (category) VALUES ('$category')";
echo $sql; //you can copy this from the screen and paste into phpmyadmin

$result = mysql_query($sql);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

It's really odd that you get ouptut to the screen with the script stuff. You may have some weird invisible characters in there. You could try creating a new file, typing out the code again and then replacing the old one - no copy/paste.

The phpmyadmin idea is also impt, you'll probably get more info from that.

^thank you! i will try this and update on what will happen.

i know right? i don't even know what happened because the last time i run the codes it was perfectly fine..and then last night that stuff appeared. =__= i've installed 3 old versions of xampp and still got the same weird ouput >.<

but i'll try this one ;)

I tried it and it still outputs the same :(

I think it has got something to do with the format of the characters? can you give me an advice to this?

Member Avatar for diafol

I assume the following:

1) You've checked that Apache and MySQL server are running.
2) PHP is actually able to run (try echo 'hello world';)
3) You've changed the code to the one suggested in a clean (new) file.
4) You get the echoed INSERT INTO tblcategory (category) VALUES ('newcat') to the screen (where 'newcat' is the new category placed in the form).
5) You've pasted this SQL into phpmyadmin to see if it works.

That right?

Hi @diafol

  1. yep both are running
  2. I tried it on a new file and it doesn't output anything.
  3. I tried it and it outputs the whole code.
  4. doesn't echo anything
  5. worked when pasted manually on phpmyadmin

WAH I don't know what's happening ;_____;

Member Avatar for diafol

OK, the fail on #2 suggests php is the problem. Odd, I can't suggest much without more info. Is this a XAMPP installation?

Yes it's xampp, I always use XAMPP, it is what I use before my laptop got stolen and I had to start all over again in this desktop ;__; I swear ALL were perfectly running back then. I am suspecting this has something to do with this desktop? or XAMPP? I've tried installing older versions and it's still the same.

GAH. And this project will be due in tweo weeks ;___; frustration!

Member Avatar for diafol

What about the XAMPP test page? Did it appear OK once installed? Did you follow the security advice?

Hi i've already solved the problem, which is so simple. D: I've been lughing it off, my my stupid me. D:

the missing thing in my code was actuallly yhe php syntax ;(

all thiss time I was using <? echo "hello world"; ?>

when actually, it should ALWAYS be: <?php echo "hello world"; ?>

My gosh what. ;(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.