Jump to content

Script stop working


divinedesigns1

Recommended Posts

ok so im self learning php, and im reading the book head first, and in one of their excerise they said to place

//define the upload pathdefine('SK_UPLOADPATH', 'photos/');

and the connect script into a different file and then require them once in both index.php and addon.php so i create the connect.php code like this

<?phpdefine('db_host', '');define('db_user', '');define('db_password', '');define('db_name', '');?>

then i made the php file for the SK_UPLOADPATH and in both files i did

require_once 'skimg.php';require_once 'connect.php';

and it stop doing anything 0.o this is the index code

<?phprequire_once 'skimg.php';require_once 'my_connect.php';//connect to the database$conn = mysqli_connect('db_host', 'db_user', 'db_password', 'db_name');//retrieve the score data form$query = "SELECT * FROM scorekeeper";$data = mysqli_query($conn, $query);//loop through the array of data, formatting it as HTMLecho '<table>';while($row = mysqli_fetch_array($data)){//Display the score dataecho '<tr><td class="scoreinfo">';echo '<span class="score">' . $row['score'] . '</span><br/>';echo '<strong>Name: </strong>' . $row['name'] . '<br/>';echo '<strong>Date: </strong>' . $row['date'] . '</td>'; if(is_file($row['screenshot']) && filesize($row['screenshot']) > 0 ) {  echo '<td><img src="' . $row['screenshot'] . '" alt="score image" /></td></tr>';}else {  echo '<td><img src="unverified.gif" alt="unverified score" /></td></tr>';}}echo '</table>';mysqli_close($conn);?>

and this is the addon code

<?phprequire_once 'skimg.php';require_once 'my_connect.php';  if (isset($_POST['submit'])) {    // Grab the score data from the POST    $name = $_POST['name'];    $score = $_POST['score'];$screenshot = $_FILES['screenshot']['name'];    if (!empty($name) && !empty($score)) {  //Move the file to the target upload folder  $target = SK_UPLOADPATH . $screenshot;  if(move_uploaded_file($_FILES['screenshot']['tmp_name'], $target)) {	  // Connect to the database	  $conn = mysqli_connect('db_host', 'db_user', 'db_password', 'db_name');	  // Write the data to the database	  $query = "INSERT INTO scorekeeper VALUES (0, NOW(), '$name', '$score', '$screenshot')";	  mysqli_query($conn, $query);	  // Confirm success with the user	  echo '<p>Thanks for adding your new high score!</p>';	  echo '<p><strong>Name:</strong> ' . $name . '<br />';	  echo '<strong>Score:</strong> ' . $score . '</p>';   echo '<img src="' . SK_UPLOADPATH . $screenshot . '" alt="Score image" /></p>';	  echo '<p><a href="index.php"><< Back to high scores</a></p>';	  // Clear the score data to clear the form	  $name = "";	  $score = "";   $screenshot = "";	  mysqli_close($conn);  }    }    else {	  echo '<p class="error">Please enter all of the information to add your high score.</p>';    }  }?>

kindly let me know if im making mistake anywhere in these code, please and thank you

Edited by DDs1
Link to comment
Share on other sites

The connection is supposed to use the constants in the configuration file (connect.php).But by using apostrophes, you're instead writing those as actual hostname, username and password, and I'd assume those aren't valid ones.In other words, replace

$conn = mysqli_connect('db_host', 'db_user', 'db_password', 'db_name');

with

$conn = mysqli_connect(db_host, db_user, db_password, db_name);

And make sure you enter the appropriate values in connect.php of course.

Link to comment
Share on other sites

oooo so you remove the single quotes and just leave the database info with the commons hehehehe i feel silly :good: thanks onces again boen

Link to comment
Share on other sites

Not just the database info... any time you want to get the value of a constant, you use its name, without quotes. And any time you intend to write a string rather than get a constant's value, you do add single (or double) quotes.

Link to comment
Share on other sites

ok so constants are define('db_host', ''); etc and strings would be the normal way of writing it like $db_host = "blahblah"; correct?

Link to comment
Share on other sites

The define() function (at least in the above example) takes two strings as arguments - a name and a value. Each of these strings is written in "the normal way".A variable declaration doesn't exactly take strings. You just have "$", followed by name of the variable, followed by "=", followed by the value for that variable. The value could be a string (written in "the normal way"), integer, float, etc. or it could be the value of a constant.Consider for example:

define('db_host', 'localhost');$db_host = db_host;

$db_host would have the value

localhost

because that is what the value of the constant "db_host" is. If you instead have

define('db_host', 'localhost');$db_host = 'db_host';

The value of $db_host is

db_host

because that's what the string on the second line says.

Link to comment
Share on other sites

thank you very much boen, now i can start back studying :) sorry i took so long to reply had to help an employee

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...