简体   繁体   中英

My PHP page is not working. It is completely blank

I am working on a site to share names of songs, and I have made a recommendation form that I include in every page. This recommendation form is in HTML and leads to a PHP action page, where the information received is added to a SQL table. Here is the code:

<?php

ob_start();
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="MYPASSWORD"; // Mysql password 
$db_name="DB NAME"; // Database name 
$tbl_name="songshare"; // Table name 

// Connect to server and select databse.
$link = mysqli_connect("$host", "$username", "$password")or die("cannot connect"); 
mysqli_select_db($link, "$db_name")or die("cannot select DB");

// Define $myusername and $mypassword 
$song=$_POST['song']; 
$album=$_POST['album']; 
$artist=$_POST['artist'];
$linkitunes=$_POST['linkitunes'];
$artwork=$_POST['albumPic'];

// To protect MySQL injection (more detail about MySQL injection)
$song = stripslashes($song);
$album = stripslashes($album);
$artist = stripslashes($artist);
$song = mysqli_real_escape_string($link, $song);
$album = mysqli_real_escape_string($link, $album);
$artist = mysqli_real_escape_string($link, $artist);

$sql="SELECT * FROM $tbl_name WHERE song='$song'";
$result=mysqli_query($link, $sql);
if ($result->num_rows){
  echo "Song already taken" . "<br />";
  echo "<a href='/music.php'>music</a>";
  exit();
}

$sql="INSERT INTO recommendation (user_id, artist, song, album, artwork, linkitunes)";
$sql = $sql . " VALUES ('$_SESSION['user_id']', '$artist', '$song', '$album'. '$artwork'. '$linkitunes');";
$result=mysqli_query($link, $sql);

if(!$result) {
   echo "Recommendation failed" . "<br />";
   echo $sql;
} else {
    print "$song, $artist, $album";
}
ob_end_flush();

?>

I have checked that every username, password, link is correct and valid. My server does, in fact, run PHP. It doesn't seem to me like the PHP code is even running though.

Thank you so much in advance.

-Cameron

Turn on error reporting by adding this on top of page:

ini_set("display_errors",true);

and change this line:

$link = mysqli_connect("$host", "$username", "$password")

to

$link = mysqli_connect($host, $username, $password,$db_name);

Please have a look how to work with mysqli

Instead of '$album'. '$artwork'. '$linkitunes' '$album'. '$artwork'. '$linkitunes' '$album'. '$artwork'. '$linkitunes' Do: '$album', '$artwork', '$linkitunes' , while saving data.

Try this :-

$sql = $sql . " VALUES ('".$_SESSION['user_id']."', '$artist', '$song', '$album', '$artwork', '$linkitunes')";

instead of

$sql = $sql . " VALUES ('$_SESSION['user_id']', '$artist', '$song', '$album'. '$artwork'. '$linkitunes');";

You should check the version of local server you are working with. If you are working with a higher of local server and you php was written in a lower version it throws a blank page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM