dani33l_87 Posted December 8, 2012 Share Posted December 8, 2012 Hi, I have the next exercise to make it and I need some help. "The following getArtist.php script is generating a prepared statement object, named: stmt. In the actual script only theArtisName is bound into the statement in the “?” place. The script on the next page has getArtist.php as its action property. Your task now is to change the two scripts to enable a full insert of all fields in the artist table. I.e. the form has to have text fields for all relevant artist data." <?php $theUserName = $_POST["userName"]; print "<h3>Hello user: $theUserName!</h3>"; $thePassword = $_POST["password"]; $theArtistName = $_POST["artistName"]; print "<h3>You are searching data for: $theArtistName!</h3>"; $mySqli = new Mysqli('localhost', $theUserName, $thePassword, 'cd_inventory'); /* check connection by means of the connect_errno property */ if ($mySqli->connect_errno) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } else { echo 'Now connected to MySQL'; echo '<br><br>'; } /* Spawn a MySQLi_STMT class object containing the SQL statement */ if ($stmt = $mySqli->prepare("SELECT artist_id, artist_name, place_of_birth FROM artists WHERE ARTIST_NAME =?")) { /* bind parameters for ? marker(s) */ $stmt->bind_param("s", $theArtistName); // s for string /* execute query */ $stmt->execute(); /* bind result variables */ $stmt->bind_result($artist_id, $artist_name, $place_of_birth); /* fetch values */ while ($stmt->fetch()) { printf("Artist: %s %s %s \n", $artist_id, $artist_name, $place_of_birth); } // else emit an error message /* close statement */ $stmt->close(); } /* close connection */ $mySqli->close(); ?> Second code: <html> <head> <title>Input Username and password</title> </head> <body> <h1>Input Username and password</h1> <form method = "post" action = "getArtist.php"> Please type your username: <input type = "text" name = "userName" value = ""> <br> Please type your password: <input type = "password" name = "password" value = ""> <br> Please type the artist you are looking for: <input type = "text" name = "artistName" value = ""> <br> <input type = "submit" value = "Submit"> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/271761-oop-php-prepared-statement-properties-passing-parameters-and-method-calls/ Share on other sites More sharing options...
trq Posted December 8, 2012 Share Posted December 8, 2012 Do you have a question? Also, if your going to post code, we have tags. Use them. Quote Link to comment https://forums.phpfreaks.com/topic/271761-oop-php-prepared-statement-properties-passing-parameters-and-method-calls/#findComment-1398274 Share on other sites More sharing options...
Andy123 Posted December 9, 2012 Share Posted December 9, 2012 It would be better if you would ask questions instead of just handing out an exercise and expect people to solve it for you. You should show that you have given it some thought and let people know which kind of problems you are facing. Where did you get stuck? Add fields for getting relevant artist data to your HTML form Get these variables from the $_POST array like already done at the top of your script Perhaps validate the values. The reason why I say perhaps is that given that you have been given this explicit task, I am assuming that this code will never go into production. Otherwise this should generally be done. Make sure that your database table can store the values Write an insert query with question marks as placeholders for your values Bind the parameters with the values from step 2 Quote Link to comment https://forums.phpfreaks.com/topic/271761-oop-php-prepared-statement-properties-passing-parameters-and-method-calls/#findComment-1398297 Share on other sites More sharing options...
dani33l_87 Posted December 9, 2012 Author Share Posted December 9, 2012 Andy you are right. I have added new fields ID <input type = "text" name = "artist_id" value = ""> NAME <input type = "text" name = "artist_name" value = ""> PLACE <input type = "text" name = "place_of_birth" value = ""> and also make the post: $stmt = $_POST["artist_id"]; $stmt = $_POST["artist_name"]; $stmt = $_POST["place_of_birth"]; I don t understand how this suppose to work and the bind parameters (I skip some classes) Quote Link to comment https://forums.phpfreaks.com/topic/271761-oop-php-prepared-statement-properties-passing-parameters-and-method-calls/#findComment-1398414 Share on other sites More sharing options...
Andy123 Posted December 9, 2012 Share Posted December 9, 2012 $artist_id = (int) $_POST['artist_id']; $artist_name = $_POST['artist_name']; $place_of_birth = $_POST['place_of_birth']; // Connect to database here $stmt = $mySqli->prepare("INSERT INTO artist (artist_id, artist_name, place_of_birth) VALUES (?, ?, ?)"); $stmt->bind_param('i', $artist_id); $stmt->bind_param('s', $artist_name); $stmt->bind_param('s', $place_of_birth); $stmt->execute(); // Do other things here Quote Link to comment https://forums.phpfreaks.com/topic/271761-oop-php-prepared-statement-properties-passing-parameters-and-method-calls/#findComment-1398417 Share on other sites More sharing options...
dani33l_87 Posted December 9, 2012 Author Share Posted December 9, 2012 Your statement will add new artist in the database? $stmt = $mySqli->prepare("INSERT INTO artist (artist_id, artist_name, place_of_birth) VALUES (?, ?, ?)"); Fatal error: Call to a member function bind_param() on a non-object instead of the original one if ($stmt = $mySqli->prepare("SELECT artist_id, artist_name, place_of_birth FROM artists WHERE ARTIST_NAME =?")) { Quote Link to comment https://forums.phpfreaks.com/topic/271761-oop-php-prepared-statement-properties-passing-parameters-and-method-calls/#findComment-1398426 Share on other sites More sharing options...
dani33l_87 Posted December 9, 2012 Author Share Posted December 9, 2012 is ok i fix it, thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/271761-oop-php-prepared-statement-properties-passing-parameters-and-method-calls/#findComment-1398444 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.