Jump to content

Oop Php, Prepared Statement, Properties, Passing Parameters And Method Calls


dani33l_87

Recommended Posts

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>

Link to comment
Share on other sites

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?

  1. Add fields for getting relevant artist data to your HTML form
  2. Get these variables from the $_POST array like already done at the top of your script
  3. 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.
  4. Make sure that your database table can store the values
  5. Write an insert query with question marks as placeholders for your values
  6. Bind the parameters with the values from step 2

Link to comment
Share on other sites

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)

Link to comment
Share on other sites


$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

Link to comment
Share on other sites

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 =?")) {

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.