Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Problem with my 1st PhP code


  • Please log in to reply

#1
lillya

lillya

    Member

  • Member
  • PipPip
  • 22 posts
Hi! I am trying to get my very first PhP code to work, but I just can't seem to get it right. My browser just keeps saying something went wrong. Plus, I have no idea why it's not working... Any suggestions would be greatly appreciated!

The code I am using is:

[codebox]<?php

$con = mysql_connect("db2.awardspace.com", "username", "password");

$result1 = mysql_query($con) or error_log(mysql_error());

mysql_select_db("lisjar_study");

$state=$_POST['state'];
$gender=$_POST['gender'];
$age=$_POST['age'];
$ethnicity=$_POST['ethnicity'];
$language=$_POST['language'];
$idnumber=$_POST['idnumber'];

$query = mysql_query("insert into participant(idnumber, state, gender, age, ethnicity, language)values('$idnumber', '$state', '$gender', '$age', '$ethnicity', '$language')");

if (mysql_affected_rows() == 1) {echo 'Your information was saved';}
else {error_log(mysql_error());
echo 'Something went wrong';}

?>[/codebox]
  • 0

Advertisements


#2
mpfeif101

mpfeif101

    Member 1K

  • Retired Staff
  • 1,411 posts
Try changing this line:

mysql_select_db("lisjar_study");

to

mysql_select_db("lisjar_study", $con);

Also make sure the username/password/other information to connect to the db is correct.
  • 0

#3
lillya

lillya

    Member

  • Topic Starter
  • Member
  • PipPip
  • 22 posts
Thanks for the advice. I am still getting the same message in my browser "something went wrong". Why am I not getting and SQL based errors? Shouldn't I if there is a problem with my code?

Also, is it possible that my code is correct and it is just a problem with something in the MySQL database? I double checked and all of my variable names are correct... could it be something else?

Thanks :whistling:
  • 0

#4
mpfeif101

mpfeif101

    Member 1K

  • Retired Staff
  • 1,411 posts
What are the error messages you are getting?
  • 0

#5
lillya

lillya

    Member

  • Topic Starter
  • Member
  • PipPip
  • 22 posts
I am not getting any error messages from MySQL. The only thing that shows up in my browser is "Something went wrong" which is because I prompted that to show up as part of my code. If I delete that echo line than my browser appears blank after I press the submit button. If you want to check it out yourself, here is the link:

http://jaremka.freeh...stionnaire.html

Thanks!
  • 0

#6
mpfeif101

mpfeif101

    Member 1K

  • Retired Staff
  • 1,411 posts
Change

if (mysql_affected_rows() == 1) {echo 'Your information was saved';}

to

if (mysql_affected_rows() >= 1) {echo 'Your information was saved';}

What your code is saying is that if only one thing is changed then it will say your information is saved, but anything else and it's gonna say something went wrong.
  • 0

#7
lillya

lillya

    Member

  • Topic Starter
  • Member
  • PipPip
  • 22 posts
Thanks again fo the suggestion. Unfortunately my browser is still saying "something went wrong." Plus, when I check my database to see if the information saved, nothing is there... :whistling:

Any other ideas???

Is it possible it is not a problem with the PhP code but instead with my database or the html in the page??
  • 0

#8
ScHwErV

ScHwErV

    Member 5k

  • Retired Staff
  • 21,285 posts
  • MVP
"Something went wrong" is not a normal error for any browser that I have seen. Can you get us the real error?
  • 0

#9
burnoutnotfadeaway

burnoutnotfadeaway

    Member

  • Member
  • PipPipPip
  • 110 posts
I think 'something went wrong' is a browser output message he has put at the end of the code. Basically, if the user does not enter info and clicks Submit it should say 'Something Went Wrong'. But even when you do enter characters into the textfields it still says 'Something Went Wrong' - the code is effectively not capturing the users input into the textfields.
  • 0

#10
ScHwErV

ScHwErV

    Member 5k

  • Retired Staff
  • 21,285 posts
  • MVP
IMO if you include error codes as vague as "Something went wrong" you deserve to have trouble.
  • 0

Advertisements


#11
lillya

lillya

    Member

  • Topic Starter
  • Member
  • PipPip
  • 22 posts
No, I asked for the error message that PhP could give me with "else {error_log(mysql_error());" which should give me some type of error message... right? But it is not telling me anything... there are no error messages at all... The 'something went wrong' was meant primarily for someone else using my site, not for use in debugging.

BTW there is no reason to be mean to someone who is just learning :whistling:
  • 0

#12
mpfeif101

mpfeif101

    Member 1K

  • Retired Staff
  • 1,411 posts
Hi there,

ScHwErV wasn't trying to be mean :whistling:

Let's fix this problem. Try this...

<?php

$username = "PUT YOUR USERNAME HERE";
$password = "PUT YOUR PASSWORD HERE";
$dbname = "lisjar_study"
$dbh = mysql_connect("localhost", $username, $password) or die (mysql_error());
$selected = mysql_select_db($dbname, $dbh) or die (mysql_error());

if isset ($_POST) { //this basically just checks to see if the form was actually submitted, to make sure that someone didnt just come to the URL directly

$state = $_POST['state'];
$gender = $_POST['gender'];
$age = $_POST['age'];
$ethnicity = $_POST['ethnicity'];
$language = $_POST['language'];
$idnumber = $_POST['idnumber'];

mysql_query("INSERT INTO participant SET idnumber='$idnumber', state='$state', gender='$gender', age='$age', ethnicity='$ethnicity', language='$language'") or die (mysql_error());

echo "Your information was saved";

}

?>

  • 0

#13
lillya

lillya

    Member

  • Topic Starter
  • Member
  • PipPip
  • 22 posts
Thanks so much for the suggestion! I was beginning to think I would never get this to work :whistling: I tried the code you suggested and am getting the following error message:

Parse error: parse error, unexpected T_ISSET, expecting '(' in /home/www/jaremka.freehostia.com/insertparticipant.php on line 18

Line 18 in my file is the line right before the isset command.

Do you have any idea what the problem may be? I tried figuring it out myself, but no luck...

Thanks!!!
  • 0

#14
mpfeif101

mpfeif101

    Member 1K

  • Retired Staff
  • 1,411 posts
Ahh, oops, shoulda double checked my code :whistling:

change this part:

if isset ($_POST) {

to

if (isset ($_POST)) {

  • 0

#15
lillya

lillya

    Member

  • Topic Starter
  • Member
  • PipPip
  • 22 posts
OMG IT WORKED!!!! Yay!!!! I am so excited it finally worked!!! Thank you so much for your help. I have been trying to get help on this for over a month now and finally it worked!!! I really appreciate your help :whistling:

Can I bother you with one last question? The variable I am using 'idnumber' I actually want to be assigned automatically to people filling out my form since it is an anonymous way to identify each person filling out the form. My MySQL database is already set for this variable to be the primary key and be autonumbered. Is there anything else I have to do? Do I even need this in my form at all? I am not sure if my question makes sense, but hopefully it does.

Again, thank you soooo much for your help!
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP