We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,390 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

[HELP] What happened to my codes? Help!

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!

7
Contributors
17
Replies
6 Days
Discussion Span
7 Months Ago
Last Updated
18
Views
Question
Answered
kitschkath
Light Poster
42 posts since Feb 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

your db connection doesnt contain a password

Squidge
Posting Pro in Training
413 posts since Dec 2009
Reputation Points: 111
Solved Threads: 62
Skill Endorsements: 5

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

kitschkath
Light Poster
42 posts since Feb 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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

Squidge
Posting Pro in Training
413 posts since Dec 2009
Reputation Points: 111
Solved Threads: 62
Skill Endorsements: 5

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

Javvy
Junior Poster
150 posts since Feb 2011
Reputation Points: 10
Solved Threads: 31
Skill Endorsements: 2

can you show us the errors????

ome2012
Junior Poster in Training
57 posts since Sep 2012
Reputation Points: 2
Solved Threads: 16
Skill Endorsements: 1

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.

pritaeas
Posting Prodigy
Moderator
9,316 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,467
Skill Endorsements: 86

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.

pritaeas
Posting Prodigy
Moderator
9,316 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,467
Skill Endorsements: 86

so what am I going to do with this?

kitschkath
Light Poster
42 posts since Feb 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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.

diafol
Keep Smiling
Moderator
10,672 posts since Oct 2006
Reputation Points: 1,632
Solved Threads: 1,514
Skill Endorsements: 57

^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 ;)

kitschkath
Light Poster
42 posts since Feb 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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?

kitschkath
Light Poster
42 posts since Feb 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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?

diafol
Keep Smiling
Moderator
10,672 posts since Oct 2006
Reputation Points: 1,632
Solved Threads: 1,514
Skill Endorsements: 57

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 ;_____;

kitschkath
Light Poster
42 posts since Feb 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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

diafol
Keep Smiling
Moderator
10,672 posts since Oct 2006
Reputation Points: 1,632
Solved Threads: 1,514
Skill Endorsements: 57

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!

kitschkath
Light Poster
42 posts since Feb 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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

diafol
Keep Smiling
Moderator
10,672 posts since Oct 2006
Reputation Points: 1,632
Solved Threads: 1,514
Skill Endorsements: 57

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. ;(

kitschkath
Light Poster
42 posts since Feb 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 7 Months Ago by diafol, pritaeas, Squidge and 2 others

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1234 seconds using 2.85MB