Jump to content

[SOLVED] Adding data to mysql database with php


jlgray48

Recommended Posts

I'm having some difficulty understanding the code below if someone could help. This is actually for an upcoming final where we have to find errors in code. Thanks for any help.

 

Question: Find the errors in the code below. This code must add a record in a table called table_1 that is located in a database called DATABASE_1.

 

<?

 

$conn = mysql_connect ("computer_1", "john_adam", "tigerismycat" ) ;;

mysql_select_db ("database_1", $conn);

$field1 = $_REQUEST["field_1"];

$field2 = $_REQUEST["field_2"];

$sql = "SELECT * FROM table_1" ;

If (mysql_query ($sql, $conn) ) {

      Print "New record was added";

} ELSE {

      Print "New record entry fails!" :

}

?>

 

I understand the 2 connection lines, I don't understand the $field lines.

 

Thanks for any help.

 

 

 

Link to comment
Share on other sites

Guest prozente

Not meaning to be rude or anything but if this is for an upcoming final(meaning the end of the class) shouldn't you of obtained this knowledge by now

Link to comment
Share on other sites

proz: I think he came for help not for a lecture.

 

@topic:

 

      Print "New record entry fails!" :

 

Should be

 

      Print "New record entry fails!";

 

Also, the SQL String is saying 'SELECT' when it should be 'INSERT INTO'

 

$_REQUEST = the same as $_POST or $_GET, it just doesn't define which one (for pages allowing both POST and GET requests.)

 

I am assuming the professor wants the code to look something like

<?php
$conn = mysql_connect ("computer_1", "john_adam", "tigerismycat" ) ;;
mysql_select_db ("database_1", $conn);
$field1 = $_REQUEST["field_1"];
$field2 = $_REQUEST["field_2"];
$sql = "INSERT INTO table_1 values('$field1', '$field2')" ;
If (mysql_query ($sql, $conn) ) {
      Print "New record was added";
} ELSE {
      Print "New record entry fails!";
}
?>

 

Link to comment
Share on other sites

Did I say this was homework? No, it is for a final and I am making sure that my work is correct if that is ok with you. I am definitely behind but that is because my father passed away 2 weeks ago and I haven't been able to attend a nite class for 2 weeks. I will be posting several more questions in the next few days. I would appreciate it if you would just ignore them. I don't have time to waste with smart SmartA$$ like you.

Link to comment
Share on other sites

He came her so someone else would do his homework for him so he does not have to learn anything.  Smart move by him, suckered one guy in.

Did I say this was homework? No, it is for a final and I am making sure that my work is correct if that is ok with you. I am definitely behind but that is because my father passed away 2 weeks ago and I haven't been able to attend a nite class for 2 weeks. I will be posting several more questions in the next few days. I would appreciate it if you would just ignore them. I don't have time to waste with smart SmartA$$ like you.

Sad sap stories always tug at me especially ones over the internet :cries:  Have fun bud.

jlgray, watch your language first of all, nobody here is entitled to help, they do it by choice.  Frost, I will say that was kinda harsh, though.  Anyways, there's no need to argue, it's over.  He got his answer and he can now go on with his final.

Link to comment
Share on other sites

Thanks Glyde. I post on message boards quite often. That's how I have learned most of what I know about programming. When you get on message boards there is always 2 or 3 posters like this guy who wants to chime in with worthless posts. My response to them is always, if you don't want to offer help then why do you sit around on message boards in the first place. I help other people on network and Cisco forums and I only go there to help not hinder. Who cares if the information the person is posting is for homework. They came here not knowing and hopefully left knowing. Isn't that what getting educated is about? Enough spewing, I've got to put up another question, I'm stuck again.

 

Thanks again......

 

jg

Link to comment
Share on other sites

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.