newhip Posted February 22, 2013 Share Posted February 22, 2013 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> Quote Link to comment https://forums.phpfreaks.com/topic/274827-referencing-rows/ Share on other sites More sharing options...
Barand Posted February 22, 2013 Share Posted February 22, 2013 If you have just inserted the record using VALUES ('$title','$path','$desc') why do you then query the table to get the values for title and desct? Quote Link to comment https://forums.phpfreaks.com/topic/274827-referencing-rows/#findComment-1414218 Share on other sites More sharing options...
newhip Posted February 22, 2013 Author Share Posted February 22, 2013 To echo it later on in the page. Is that bad practice? Quote Link to comment https://forums.phpfreaks.com/topic/274827-referencing-rows/#findComment-1414220 Share on other sites More sharing options...
waynew Posted February 22, 2013 Share Posted February 22, 2013 (edited) 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 February 22, 2013 by waynewex Quote Link to comment https://forums.phpfreaks.com/topic/274827-referencing-rows/#findComment-1414224 Share on other sites More sharing options...
Barand Posted February 22, 2013 Share Posted February 22, 2013 It's an inefficient use of resources when you already have the values you want in $title and $desc. You would only need to do that on another page Quote Link to comment https://forums.phpfreaks.com/topic/274827-referencing-rows/#findComment-1414225 Share on other sites More sharing options...
newhip Posted February 22, 2013 Author Share Posted February 22, 2013 what does the blurp.php page do? or is it just an example of something i could be linking to? Quote Link to comment https://forums.phpfreaks.com/topic/274827-referencing-rows/#findComment-1414230 Share on other sites More sharing options...
waynew Posted February 22, 2013 Share Posted February 22, 2013 (edited) 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 February 22, 2013 by waynewex Quote Link to comment https://forums.phpfreaks.com/topic/274827-referencing-rows/#findComment-1414231 Share on other sites More sharing options...
waynew Posted February 22, 2013 Share Posted February 22, 2013 what does the blurp.php page do? or is it just an example of something i could be linking to? Just an example page you could link to. Quote Link to comment https://forums.phpfreaks.com/topic/274827-referencing-rows/#findComment-1414232 Share on other sites More sharing options...
newhip Posted February 22, 2013 Author Share Posted February 22, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/274827-referencing-rows/#findComment-1414233 Share on other sites More sharing options...
waynew Posted February 22, 2013 Share Posted February 22, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/274827-referencing-rows/#findComment-1414236 Share on other sites More sharing options...
newhip Posted February 22, 2013 Author Share Posted February 22, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/274827-referencing-rows/#findComment-1414243 Share on other sites More sharing options...
Barand Posted February 22, 2013 Share Posted February 22, 2013 (edited) 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 } Edited February 22, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/274827-referencing-rows/#findComment-1414254 Share on other sites More sharing options...
newhip Posted February 22, 2013 Author Share Posted February 22, 2013 I'm kinda really confused now what is the blurb.php file now? is it because i shouldnt be displaying on the page that process's the upload? I still dont understand how to reference the page in the url Quote Link to comment https://forums.phpfreaks.com/topic/274827-referencing-rows/#findComment-1414262 Share on other sites More sharing options...
Barand Posted February 22, 2013 Share Posted February 22, 2013 (edited) 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 Edited February 22, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/274827-referencing-rows/#findComment-1414270 Share on other sites More sharing options...
newhip Posted February 22, 2013 Author Share Posted February 22, 2013 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/274827-referencing-rows/#findComment-1414274 Share on other sites More sharing options...
Barand Posted February 22, 2013 Share Posted February 22, 2013 I still dont understand how to reference the page in the url <a href="blurb.php?id=<?php echo $id; ?>"> where "blurb.php" is whatever page you want to link to and the id is passed in the query string (eg ?id=123) Quote Link to comment https://forums.phpfreaks.com/topic/274827-referencing-rows/#findComment-1414279 Share on other sites More sharing options...
newhip Posted February 22, 2013 Author Share Posted February 22, 2013 Ok sry Barand I'm still a little confused please bare with me ... Are you saying that i would need to create a new page that is linked to upload.php to be able to refenrence the data in the table? Quote Link to comment https://forums.phpfreaks.com/topic/274827-referencing-rows/#findComment-1414296 Share on other sites More sharing options...
Barand Posted February 22, 2013 Share Posted February 22, 2013 No No No I am saying you can, not need 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. We are attempting to answer that question for you. Quote Link to comment https://forums.phpfreaks.com/topic/274827-referencing-rows/#findComment-1414300 Share on other sites More sharing options...
newhip Posted February 22, 2013 Author Share Posted February 22, 2013 oh ok but if the id is 123 i should be able the access the populated page under www.example.com/upload.php?id=123 ? Quote Link to comment https://forums.phpfreaks.com/topic/274827-referencing-rows/#findComment-1414302 Share on other sites More sharing options...
Barand Posted February 22, 2013 Share Posted February 22, 2013 How can it when you don't know the id until after the upload? Quote Link to comment https://forums.phpfreaks.com/topic/274827-referencing-rows/#findComment-1414305 Share on other sites More sharing options...
newhip Posted February 23, 2013 Author Share Posted February 23, 2013 No i mean if i upload and then want to reference it later or something older in the database Quote Link to comment https://forums.phpfreaks.com/topic/274827-referencing-rows/#findComment-1414311 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.