Forum Moderators: coopster
Codes on my index.php:
<?php
$HOST = 'localhost';
$USERNAME = 'root';
$PASSWORD = '';
$DB = 'c203';
$link = mysqli_connect($HOST,$USERNAME,$PASSWORD,$DB) or die(mysqli_connect_error());
$sql = "SELECT name,birthday,nationality,age,biography,picture FROM stars";
$result = mysqli_query($link, $sql) or die(mysqli_error($link));
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>StarGazer</title>
</head>
<body>
<h1>StarGazer</h1>
<hr />
<h2>Welcome</h2>
<table border='1' cellpadding='0' cellspacing='0'>
<?php
while($row=mysqli_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$row['picture']."</td>";
echo "<img src='picture/" ;
echo "</tr>";
//use $row and IMG SRC
}
?>
</table>
</body>
</html>
Codes on my addstars.php
<?php
// find the code that only do INSERT
$name = $_POST['name'];
$birthday = $_POST['birthday'];
$nationality = $_POST['nationality'];
$age = $_POST['age'];
$biography = $_POST['biography'];
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['upfile']['name']);
if(move_uploaded_file($_FILES['upfile']['tmp_name'], $target_path . basename ( $_FILES['upfile']['name']))) {
echo "The file ". basename( $_FILES['upfile']['name']). " has been uploaded. Please click <a href=index.php>here </a> ";
}
else{
echo "There was an error uploading the file, please try again!";
}
$host = 'localhost';
$username = 'root';
$password = '';
$db = 'c203';
$link = mysqli_connect($host,$username,$password,$db);{
$query = "INSERT INTO stars(name,birthday, nationality,age,biography,picture)
VALUES('$name','$birthday','$nationality','$age','$biography','$target_path')";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
}
$message = "Name : " . $name . "<br />";
$message1 = "Birthday : " . $birthday . "<br />";
$message2 = "Nationality : " . $nationality . "<br />";
$message3 = "Age : " . $age. "<br />";
$message4 = "Biography : " . $biography . "<br />";
?>
My Gratitude
K well I am going under the presumption you mean broken picture icon, not link. There's both, and I only see a problem with the image. But you have other problems as well.
First,
$sql = "SELECT name,birthday,nationality,age,biography,picture FROM stars";
then we wind our way down through the code and find
echo "<tr>";
echo "<td>" . $row['picture'] . "</td>";
echo "<img src='picture/" ;
echo "</tr>";
Two things here. The <img> tag is incomplete, no closing </td>. The quoting is also incomplete.
echo "<img src=\"picture/" . $row['picture'] . "\" alt=\"" . $row['picture'] . \""></td>";
There are no size attributes, but ok . . . what you really should do here is make sure you access this from root, note the leading slash.
echo "<img src=\"/picture/" . $row['picture'] . "\" alt=\"" . $row['picture'] . \""></td>";
If you mean this,
Please click <a href=index.php>here </a>
Three things:
- The index.php has to be relative to wherever you are now. Again, use the leading slash.
- Quote your links. There are good reasons.
- "Click here . . . " for what? What does that tell me about what the link does? It tells me how to operate the hyperlink, I already know that. :-) Make your link helpful.
echo "<p>Return to the <a href=\"index.php\">home page</a></p>";
Last, you are doing the response before the insert:
echo "The file ". basename( $_FILES['upfile']['name']). " has been uploaded. Please click <a href=index.php>here </a> ";
.... then ....
$query = "INSERT INTO stars(name,birthday, na . . .
. . . and it looks like you go on to compose $message for an email, in XHTML style syntax when you could easily use paragraphs (but, do as ye may there.)
The point is, this leaves you with no way to error trap either of these tasks, you've already output to the browser. You need to rearrange it like this:
- accept and check INPUT
- If problems or errors on input alone*, report to browser, terminate script
- otherwise,
-- perform upload, if error, DELETE any uploaded files, report error, terminate script
-- no errors on upload, do the DB insert, if error - you're probably getting the trend, delete uploaded files, report, terminate
-- no errors on upoad, send email, if error, delete uploaded file, delete entries, report, terminate
-- NOW after all has been done, return a response to the user.
* A note on "usability:" I see this all the time in PHP, and I've no idea why this is a standard operating mode: write a script, post to another. Let that post to another. Post to another, redirect. Store states in an elaborate collection of $_SESSION variables.
What you **really** should do here, is keep it all in one script, so on error, you can easily return to the original form with the user's input values intact, rather than posting from script one to script 2. A typical scenario,
if ($_POST['some-form_element']) {
$errors = checkData();
if ($errors) { outputForm($errors); }
else{
processData();
sendEmail();
successResponse();
}
}
else { outputForm(); }
That's all there is, for 90% of the scripts I see. If input, check, if error, return to form, otherwise do your functions, respond. If no input, output form.
This will build much more efficient apps for you and make them portable and maintainable . . . it's never too late. :-)