Jump to content

Referencing rows


newhip
 Share

Recommended Posts

Ok im a super newb and i dont know how to to reference the data in my mysql table. I wrote a code that uploads a file to a server then to a database where its echoed out afterwards. The code also echoes out the id in the table's field so i know the id but i dont know how to afterwards reference it in the url. It's prob really basic but i can't figure it out. Can someone help me out or hint. Would be very appreciated. Here's the code

 

<?php
$conn=mysql_connect("localhost","root","");
$select=mysql_select_db("project",$conn);
$title=$_POST['title'];
$pic=$_FILES['pic']['tmp_name'];
$pic2=$_FILES['pic']['name'];
$desc=$_POST['desc'];
$path="upload/".$pic2;
$themove=move_uploaded_file($pic,$path);
$sqlqry=mysql_query("INSERT INTO media(title,pic,desct) VALUES ('$title','$path','$desc')");
echo $hmmm=mysql_insert_id($conn);
$searr="SELECT * FROM media WHERE id='$hmmm'";
$seartt=mysql_query($searr);
$burp=mysql_fetch_array($seartt);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New Uploaded Page</title>
</head>


<body>
<a href="#"><h1><?php  echo $burp['title'];  ?></h1></a><br /><br />
<img src="<?php
  echo $burp['pic'];

?>" /><br /><p><br /><?php
echo $burp['desct'];
?></p>
</body>
</html>

Link to comment
Share on other sites

Change

 

<a href="#"><h1><?php echo $burp['title']; ?></h1></a>

 

to

 

<h1><a href="blurb.php?id=<?php echo $hmmm; ?>"><?php echo $burp['title']; ?></a></h1>

 

Note that you'll have to create a page called blurb.php

Edited by waynewex
Link to comment
Share on other sites

Barand is right:

 

<?php
$conn = mysql_connect("localhost", "root", "");
$select = mysql_select_db("project", $conn);


$title = mysql_real_escape_string($_POST['title'], $conn);
$pic = $_FILES['pic']['tmp_name'];
$pic2 = $_FILES['pic']['name'];
$desc = mysql_real_escape_string($_POST['desc'], $conn);
$path = mysql_real_escape_string("upload/".$pic2, $conn);


$themove = move_uploaded_file($pic,$path);


$sqlqry = mysql_query("INSERT INTO media(title,pic,desct) VALUES ('$title','$path','$desc')", $conn);
$id = mysql_insert_id($conn);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New Uploaded Page</title>
</head>

<body>
   <h1>
       <a href="blurb.php?id=<?php echo $id; ?>">
           <?php echo htmlentities($title, ENT_QUOTES, "utf-8"); ?>
       </a>
   </h1>
   <br />
   <br />
   <img src="<?php echo htmlentities($path, ENT_QUOTES, "utf-8"); ?>" /><br />
   <p>
       <br />
       <?php echo htmlentities($desc, ENT_QUOTES, "utf-8"); ?>
   </p>
</body>
</html>

 

One less query...

Edited by waynewex
Link to comment
Share on other sites

Barand are you saying i should not input it in the table because im echoing it on the same page? I get that i can just echo it out on here because i grabbed the data from the form. Is that what you mean?

 

He's saying that you don't need to run the SELECT query because the data you're inserting is available to the rest of your script. Although one could argue that you should be using the Post/Redirect/Get pattern: http://en.wikipedia.org/wiki/Post/Redirect/Get

Link to comment
Share on other sites

Barand is right:

 

<?php
$conn = mysql_connect("localhost", "root", "");
$select = mysql_select_db("project", $conn);


$title = mysql_real_escape_string($_POST['title'], $conn);
$pic = $_FILES['pic']['tmp_name'];
$pic2 = $_FILES['pic']['name'];
$desc = mysql_real_escape_string($_POST['desc'], $conn);
$path = mysql_real_escape_string("upload/".$pic2, $conn);


$themove = move_uploaded_file($pic,$path);


$sqlqry = mysql_query("INSERT INTO media(title,pic,desct) VALUES ('$title','$path','$desc')", $conn);
$id = mysql_insert_id($conn);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New Uploaded Page</title>
</head>

<body>
<h1>
<a href="blurb.php?id=<?php echo $id; ?>">
<?php echo htmlentities($title, ENT_QUOTES, "utf-8"); ?>
</a>
</h1>
<br />
<br />
<img src="<?php echo htmlentities($path, ENT_QUOTES, "utf-8"); ?>" /><br />
<p>
<br />
<?php echo htmlentities($desc, ENT_QUOTES, "utf-8"); ?>
</p>
</body>
</html>

 

One less query...

 

Oh ok I think i get it. I didnt know you could do that. But like now how do i go access that page in the url with that info displayed? Like in a www.example.com/upload.php? manner?

Link to comment
Share on other sites

is it because i shouldnt be displaying on the page that process's the upload?

 

There is no reason why you shouldn't display in the page that does the upload. What I am saying is that it is a waste of time doing a SELECT query to get the data you have just uploaded because you already have that data :facewall:

Edited by Barand
Link to comment
Share on other sites

this is where you need the SELECT query

 

blurb.php

<?php

if (isset($_GET['id'])) {
$id = intval($_GET['id'];
$sql = "SELECT title, path, desct
 FROM media
 WHERE id = $id";

$result = mysql_query($sql);

// process results
}

 

Ok... but what does that have to do with the blurb.php ?

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

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.

 Share

×
×
  • 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.