nayrufyoradin Posted February 23, 2009 Share Posted February 23, 2009 Hello. The little PHP I know was due to a class I took. I thought I understood it but I guess I didn't really learn enough. I started out with one table, image_add, image_modify, image_delete, and the index page. It was for my image gallery. I created another table for my sketches and am trying to use the same pages to update it depending on which link I clicked on the index page. (Add new Gallery Item) or (Add new Sketch) So on my index page, I added on a type="sketches/gallery" to the end of the link. (This is all I do on the index page. I don't even know if that is technically acceptable but I figure it gives it something for the imaged add and modify page to reference. <a href="image_add.php?category=image?type=sketches" title="Add Image">Add Image</a> how do i create an IF statement on the image_add.php page that calls out the "type" from the url? I tried assigning the variable $type = $_GET['type'] and then later when referencing which table to go to. if ($type = "gallery"){ $sql = "INSERT INTO img_gallery "; } if ($type = "sketches"){ $sql = "INSERT INTO img_sketches "; } and then just for error checking <?php echo $type ?> to see if it was even recognizing it from the URL which it isn't.... What am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/146561-solved-trying-to-collect-data-from-the-url/ Share on other sites More sharing options...
Philip Posted February 23, 2009 Share Posted February 23, 2009 if ($type = "gallery"){ $sql = "INSERT INTO img_gallery "; } if ($type = "sketches"){ $sql = "INSERT INTO img_sketches "; } should be (notice the ==, not =): if ($type == "gallery"){ $sql = "INSERT INTO img_gallery "; } if ($type == "sketches"){ $sql = "INSERT INTO img_sketches "; } Also, here's how I would design that using a switch (makes it easier to see & add more in options in the future): <?php switch($_GET['type']) { case 'gallery': $sql = "INSERT INTO img_gallery "; break; case 'sketches': $sql = "INSERT INTO img_sketches "; break; case 'another': default: // (in case the url variable doesn't match, what should it do? // this is where default comes in really handy // for an example, we'll use one from above: $sql = "INSERT INTO img_sketches "; break; } ?> edit: fixed a few typos + added switch statement code Quote Link to comment https://forums.phpfreaks.com/topic/146561-solved-trying-to-collect-data-from-the-url/#findComment-769412 Share on other sites More sharing options...
Maq Posted February 23, 2009 Share Posted February 23, 2009 1) The first variable after the URL should be '?' which it is, everything after that should be separated by '&'. So this; Should be: 2) When comparing use double equal signs (==). Use singles for assigning. So this: if ($type = "gallery"){ should be: if ($type == "gallery"){ Quote Link to comment https://forums.phpfreaks.com/topic/146561-solved-trying-to-collect-data-from-the-url/#findComment-769414 Share on other sites More sharing options...
nayrufyoradin Posted February 23, 2009 Author Share Posted February 23, 2009 Alright, I made both of the suggestions that you guys made. When I get to the other pages. How do assign the &type=images from the URL to a variable and have it echo out right away? Figured it out: I was using () to assign the variable instead of [ ] and by switching the the & it fixed it as well... Can you explain how that style thing works a little bit more? Its really interesting but I don't know php well enough to understand it yet. Quote Link to comment https://forums.phpfreaks.com/topic/146561-solved-trying-to-collect-data-from-the-url/#findComment-769418 Share on other sites More sharing options...
Maq Posted February 23, 2009 Share Posted February 23, 2009 I'm not sure where you were using () to assign the variable when in your first post you said: I tried assigning the variable $type = $_GET['type'] Please show us the 2 different "styles" you're referring to. Quote Link to comment https://forums.phpfreaks.com/topic/146561-solved-trying-to-collect-data-from-the-url/#findComment-769424 Share on other sites More sharing options...
nayrufyoradin Posted February 23, 2009 Author Share Posted February 23, 2009 Would you mind if I posted my whole code here? Quote Link to comment https://forums.phpfreaks.com/topic/146561-solved-trying-to-collect-data-from-the-url/#findComment-769425 Share on other sites More sharing options...
Philip Posted February 23, 2009 Share Posted February 23, 2009 Thats fine Just use tags Quote Link to comment https://forums.phpfreaks.com/topic/146561-solved-trying-to-collect-data-from-the-url/#findComment-769426 Share on other sites More sharing options...
nayrufyoradin Posted February 23, 2009 Author Share Posted February 23, 2009 index.php <?php include '../../connect.php'; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xlmns="http://www.w3.org/1999/xhtml" xml:lang=en-US" lang="en-US"> <head> <title>Administrative Area</title> <meta http-equiv="Content-Type" content="text/html"; charset=iso-8859-1" /> </head> <body> <h1>Administrative Area</h1> <h2>Main Image Gallery</h2> <p><a href="image_add.php?category=image&type=gallery" title="Add Image">Add Image</a></p> <ul> <?php $sql ="SELECT id, title, small_file_name "; $sql .="FROM img_gallery "; $sql .= "ORDER BY id DESC "; $result = mysql_query($sql); while($row = mysql_fetch_assoc($result)) { $id = $row['id']; $title = $row['title']; $small_file_name = $row['small_file_name']; echo "<li><a href=\"image_modify.php?id=$id&type=gallery\" title=\"Modify Image\"><img src=\"../gallery_images/$small_file_name\" alt=\"$title\"/> <span><a href=\"image_remove.php?id=$id\" title=\"Remove Image\">Remove</a></span></li>\n"; } ?> </ul> <h2>Sketches</h2> <p><a href="image_add.php?category=image&type=sketches" title="Add Image">Add Image</a></p> <ul> <?php $sql ="SELECT id, title, small_file_name "; $sql .="FROM img_sketches "; $sql .= "ORDER BY id DESC "; $result = mysql_query($sql); while($row = mysql_fetch_assoc($result)) { $id = $row['id']; $title = $row['title']; $small_file_name = $row['small_file_name']; echo "<li><a href=\"image_modify.php?id=$id&type=sketches\" title=\"Modify Image\"><img src=\"../gallery_images/$small_file_name\" alt=\"$title\"/> <span><a href=\"image_remove.php?id=$id\" title=\"Remove Image\">Remove</a></span></li>\n"; } ?> </ul> </body> </html> image_add.php <?php $type = $_GET['type']; if(empty($_POST)) { $status = 'To add a new gallery item, fill out the form below. When you are finished, click the Add Image button once.'; } else { $title = $_POST['title']; $caption = $_POST['caption']; $category = $_POST['category']; $destination = '../gallery_images'; $small_file_name = $_FILES['small']['name']; $small_tmp_name = $_FILES['small']['tmp_name']; $large_file_name = $_FILES['large']['name']; $large_tmp_name = $_FILES['large']['tmp_name']; //print_r($_POST); //use this to show $_POST information being sent //print_r($_FILES); // use this to show $_FILES information being sent //die(); //kills rest of script $error_list = array(); if(empty($title)) { $error_list[] = 'You did not supply a Title'; } if(empty($small_file_name) && ($category == 'image')) { $error_list[] = 'You did not supply a Small Image'; } if(empty($large_file_name) && ($category == 'image')) { $error_list[] = 'You did not supply a Large Image'; } if($large_file_name == $small_file_name) { $error_list[] = 'You can not have the same file for both the Small and Large images'; } if(empty($error_list)) { include '../../connect.php'; if ($type == "gallery"){ $sql = "INSERT INTO img_gallery "; } if ($type == "sketches"){ $sql = "INSERT INTO img_sketches "; } #$sql .= "SET title='$title', description='$description', small_file_name='$small_file_name', large_file_name='$large_file_name' "; $sql .= "SET title='$title', caption='$caption' "; if(!empty($small_file_name)) { if(move_uploaded_file($small_tmp_name, "$destination/$small_file_name")) { $sql .= ", small_file_name='$small_file_name' "; } } if(!empty($large_file_name)) { if(move_uploaded_file($large_tmp_name, "$destination/$large_file_name")) { $sql .= ", large_file_name='$large_file_name' "; } } if(mysql_query($sql)) { header('Location: index.php'); } else { $status = mysql_error(); 'Unable to add your images.'; } } else { $status = '<ul>'; foreach($error_list as $error_message) { $status .= "<li>$error_message</li>"; } $status .= '</ul>'; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xlmns="http://www.w3.org/1999/xhtml" xml:lang=en-US" lang="en-US"> <head> <title>Add Image -- Administrative Area</title> <meta http-equiv="Content-Type" content="text/html"; charset=iso-8859-1" /> </head> <body> <h1>Administrative Area</h1> <h2>Add Image</h2> <h3><?php echo $type; ?></h3> <div><?php echo $status; ?></div> <form action="image_add.php" method="post" enctype="multipart/form-data"> <dl> <dt><label for="title">Title</label></dt> <dd><input type="text" name="title" id="title" value="<?php echo $title; ?>" /></dd> <dt><label for="caption">Caption</label></dt> <dd><textarea name="caption" id="caption" rows="3" cols="35"><?php echo $caption; ?></textarea></dd> <dt><label for="small">Small Image (150x180)</label></dt> <dd><input type="file" name="small" id="small" /></dd> <dt><label for="large">Large Image</label></dt> <dd><input type="file" name="large" id="large" /></dd> </dl> <div> <input type="submit" name="submit" value="Add Image" /> <input type="reset" name="reset" value="Reset Form" /> </div> </form> </body> </html> image_modify.php <?php include '../../connect.php'; if(empty($_POST)) { $status = 'To modify a image, fill out the form below. When you are finished, click the Modify Image button once.'; $id = $_GET['id']; $type = $_POST['type']; $sql = "SELECT title, caption, small_file_name, large_file_name "; if ($type == "gallery"){ $sql .= "FROM img_gallery "; } if ($type == "sketches"){ $sql .= "FROM img_sketches "; } //$sql .= "FROM img_gallery "; $sql .= "WHERE id='$id' "; $result = mysql_query($sql); $row = mysql_fetch_assoc($result); $title = $row['title']; $caption = $row['caption']; $small_file_name = $row['small_file_name']; $large_file_name = $row['large_file_name']; } else { $id = $_POST['id']; $title = $_POST['title']; $caption = $_POST['caption']; $type = $_GET['type']; $destination = '../gallery_images'; $small_file_name = $_FILES['small']['name']; $small_tmp_name = $_FILES['small']['tmp_name']; $large_file_name = $_FILES['large']['name']; $large_tmp_name = $_FILES['large']['tmp_name']; $error_list = array(); if(empty($id)) { $error_list[] = 'You did not supply a ID'; } if(empty($title)) { $error_list[] = 'You did not supply a Title'; } if(empty($error_list)) { if ($type = "gallery"){ $sql = "UPDATE img_gallery "; } if ($type = "sketches"){ $sql = "UPDATE img_sketches "; } //$sql = "UPDATE img_gallery "; $sql .= "SET title='$title', caption='$caption' "; if(!empty($small_file_name)) { if(move_uploaded_file($small_tmp_name, "$destination/$small_file_name")) { $sql .= ", small_file_name='$small_file_name' "; } } if(!empty($large_file_name)) { if(move_uploaded_file($large_tmp_name, "$destination/$large_file_name")) { $sql .= ", large_file_name='$large_file_name' "; } } $sql .= "WHERE id='$id' "; if(mysql_query($sql)) { header('Location: index.php'); } else { $status = 'Unable to update your images.'; } } else { $status = '<ul>'; foreach($error_list as $error_message) { $status .= "<li>$error_message</li>"; } $status .= '</ul>'; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xlmns="http://www.w3.org/1999/xhtml" xml:lang=en-US" lang="en-US"> <head> <title>Modify Image -- Administrative Area</title> <meta http-equiv="Content-Type" content="text/html"; charset=iso-8859-1" /> </head> <body> <h1>Administrative Area</h1> <a href="index.php">Back to Admin Index</a> <h2>Modify Image</h2> <h3>Hello<?php echo $type; ?></h3> <div><?php echo $status; ?></div> <form action="image_modify.php" method="post" enctype="multipart/form-data"> <dl> <dt><label for="title">Title</label></dt> <dd><input type="text" name="title" id="title" value="<?php echo $title; ?>" /></dd> <dt><label for="caption">Caption</label></dt> <dd><textarea name="caption" id="caption" rows="3" cols="35"><?php echo $caption; ?></textarea></dd> <dt><label for="small">Small Image</label></dt> <dd> <input type="file" name="small" id="small" /> [<a href="../gallery_images/<?php echo $small_file_name; ?>" title="View Small Image">View</a>] </dd> <dt><label for="large">Large Image</label></dt> <dd> <input type="file" name="large" id="large" /> [<a href="../gallery_images/<?php echo $large_file_name; ?>" title="View Large Image">View</a>] </dd> </dl> <div> <input type="hidden" name="id" value="<?php echo $id; ?>" /> <input type="submit" name="submit" value="Modify Image" /> <input type="reset" name="reset" value="Reset Form" /> </div> </form> </body> </html> This is pretty much the most recent of what I am working with. It does echo out the proper stuff but gives me Error: Unknown system variable 'title' When I submit on the image add page. Quote Link to comment https://forums.phpfreaks.com/topic/146561-solved-trying-to-collect-data-from-the-url/#findComment-769429 Share on other sites More sharing options...
Philip Posted February 23, 2009 Share Posted February 23, 2009 It is giving you that error because $title wasn't set because you have it being set when !empty($_POST) A workaround is on the fields have something like: <dd><input type="text" name="title" id="title" value="<?php if(isset($title)) echo $title; ?>" /></dd> Quote Link to comment https://forums.phpfreaks.com/topic/146561-solved-trying-to-collect-data-from-the-url/#findComment-769434 Share on other sites More sharing options...
nayrufyoradin Posted February 23, 2009 Author Share Posted February 23, 2009 Error: SELECT title, caption, small_file_name, large_file_name WHERE id='6' Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home1/xxx/public_html/admin/image_modify.php on line 24 image_modify.php <?php include '../../connect.php'; if(empty($_POST)) { $status = 'To modify a image, fill out the form below. When you are finished, click the Modify Image button once.'; $id = $_GET['id']; $type = $_POST['type']; $sql = "SELECT title, caption, small_file_name, large_file_name "; $type = $_POST['type']; if ($type == "gallery"){ $sql .= "FROM img_gallery "; } if ($type == "sketches"){ $sql .= "FROM img_sketches "; } //$sql .= "FROM img_gallery "; $sql .= "WHERE id='$id' "; echo $sql,'<br />'; $result = mysql_query($sql); $row = mysql_fetch_assoc($result); $title = $row['title']; $caption = $row['caption']; $small_file_name = $row['small_file_name']; $large_file_name = $row['large_file_name']; } else { $id = $_POST['id']; $title = $_POST['title']; $caption = $_POST['caption']; $type = $_GET['type']; $destination = '../gallery_images'; $small_file_name = $_FILES['small']['name']; $small_tmp_name = $_FILES['small']['tmp_name']; $large_file_name = $_FILES['large']['name']; $large_tmp_name = $_FILES['large']['tmp_name']; $error_list = array(); if(empty($id)) { $error_list[] = 'You did not supply a ID'; } if(empty($title)) { $error_list[] = 'You did not supply a Title'; } if(empty($error_list)) { if ($type = "gallery"){ $sql = "UPDATE img_gallery "; } if ($type = "sketches"){ $sql = "UPDATE img_sketches "; } //$sql = "UPDATE img_gallery "; $sql .= "SET title='$title', caption='$caption' "; if(!empty($small_file_name)) { if(move_uploaded_file($small_tmp_name, "$destination/$small_file_name")) { $sql .= ", small_file_name='$small_file_name' "; } } if(!empty($large_file_name)) { if(move_uploaded_file($large_tmp_name, "$destination/$large_file_name")) { $sql .= ", large_file_name='$large_file_name' "; } } $sql .= "WHERE id='$id' "; if(mysql_query($sql)) { header('Location: index.php'); } else { $status = 'Unable to update your images.'; } } else { $status = '<ul>'; foreach($error_list as $error_message) { $status .= "<li>$error_message</li>"; } $status .= '</ul>'; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xlmns="http://www.w3.org/1999/xhtml" xml:lang=en-US" lang="en-US"> <head> <title>Modify Image -- Administrative Area</title> <meta http-equiv="Content-Type" content="text/html"; charset=iso-8859-1" /> </head> <body> <h1>Administrative Area</h1> <a href="index.php">Back to Admin Index</a> <h2>Modify Image</h2> <h3>Hello<?php echo $type; ?></h3> <div><?php echo $status; ?></div> <form action="image_modify.php" method="post" enctype="multipart/form-data"> <dl> <dt><label for="title">Title</label></dt> <dd><input type="text" name="title" id="title" value="<?php if(isset($title)) echo $title; ?>" /></dd> <dt><label for="caption">Caption</label></dt> <dd><textarea name="caption" id="caption" rows="3" cols="35"><?php echo $caption; ?></textarea></dd> <dt><label for="small">Small Image</label></dt> <dd> <input type="file" name="small" id="small" /> [<a href="../gallery_images/<?php echo $small_file_name; ?>" title="View Small Image">View</a>] </dd> <dt><label for="large">Large Image</label></dt> <dd> <input type="file" name="large" id="large" /> [<a href="../gallery_images/<?php echo $large_file_name; ?>" title="View Large Image">View</a>] </dd> </dl> <div> <input type="hidden" name="id" value="<?php echo $id; ?>" /> <input type="submit" name="submit" value="Modify Image" /> <input type="reset" name="reset" value="Reset Form" /> </div> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/146561-solved-trying-to-collect-data-from-the-url/#findComment-769454 Share on other sites More sharing options...
Philip Posted February 23, 2009 Share Posted February 23, 2009 You have it as if(empty($_POST)) { ... but then try to call a value from post $type = $_POST['type']; So you're not getting the $type, and not getting the "FROM tablename" in your query Quote Link to comment https://forums.phpfreaks.com/topic/146561-solved-trying-to-collect-data-from-the-url/#findComment-769457 Share on other sites More sharing options...
Maq Posted February 23, 2009 Share Posted February 23, 2009 $type = $_POST['type']; I also thought you were using the GET method (grabbing the type value from the URL? So shouldn't that line be: $type = $_GET['type']; ? Quote Link to comment https://forums.phpfreaks.com/topic/146561-solved-trying-to-collect-data-from-the-url/#findComment-769460 Share on other sites More sharing options...
nayrufyoradin Posted February 23, 2009 Author Share Posted February 23, 2009 You have it as if(empty($_POST)) { ... but then try to call a value from post $type = $_POST['type']; So you're not getting the $type, and not getting the "FROM tablename" in your query To whoever else is on. How would I fix that? Quote Link to comment https://forums.phpfreaks.com/topic/146561-solved-trying-to-collect-data-from-the-url/#findComment-769461 Share on other sites More sharing options...
nayrufyoradin Posted February 23, 2009 Author Share Posted February 23, 2009 oo. Maq you were absolutely correct. I was supposed to be using $_GET... it appears to be fixed now. Quote Link to comment https://forums.phpfreaks.com/topic/146561-solved-trying-to-collect-data-from-the-url/#findComment-769463 Share on other sites More sharing options...
Maq Posted February 23, 2009 Share Posted February 23, 2009 oo. Maq you were absolutely correct. I was supposed to be using $_GET... it appears to be fixed now. Hehehe. If it is [sOLVED] please mark as so. Quote Link to comment https://forums.phpfreaks.com/topic/146561-solved-trying-to-collect-data-from-the-url/#findComment-769465 Share on other sites More sharing options...
nayrufyoradin Posted February 24, 2009 Author Share Posted February 24, 2009 Alright new issue... kind of. I just got home and am coming across a strange problem on my image_add.php This is the error I get: Unknown system variable 'title' Per suggestion earlier I included code to echo the sql statement: SET title='Unicorn', caption='Unicorn sketch I did. It was later used in a cd mock-up and clothing label I designed.' , small_file_name='ig_sk2_tn001.jpg' , large_file_name='ig_sk2_001.jpg' It does set the title... so why is it error-ing out? <?php $type = $_GET['type']; if(empty($_POST)) { $status = 'To add a new gallery item, fill out the form below. When you are finished, click the Add Image button once.'; } else { $title = $_POST['title']; $caption = $_POST['caption']; $category = $_POST['category']; $destination = '../gallery_images'; $small_file_name = $_FILES['small']['name']; $small_tmp_name = $_FILES['small']['tmp_name']; $large_file_name = $_FILES['large']['name']; $large_tmp_name = $_FILES['large']['tmp_name']; //print_r($_POST); //use this to show $_POST information being sent //print_r($_FILES); // use this to show $_FILES information being sent //die(); //kills rest of script $error_list = array(); if(empty($title)) { $error_list[] = 'You did not supply a Title'; } if(empty($small_file_name) && ($category == 'image')) { $error_list[] = 'You did not supply a Small Image'; } if(empty($large_file_name) && ($category == 'image')) { $error_list[] = 'You did not supply a Large Image'; } if($large_file_name == $small_file_name) { $error_list[] = 'You can not have the same file for both the Small and Large images'; } if(empty($error_list)) { include '../../connect.php'; if ($type == "gallery"){ $sql = "INSERT INTO img_gallery "; } if ($type == "sketches"){ $sql = "INSERT INTO img_sketches "; } #$sql .= "SET title='$title', description='$description', small_file_name='$small_file_name', large_file_name='$large_file_name' "; $sql .= "SET title='$title', caption='$caption' "; if(!empty($small_file_name)) { if(move_uploaded_file($small_tmp_name, "$destination/$small_file_name")) { $sql .= ", small_file_name='$small_file_name' "; } } if(!empty($large_file_name)) { if(move_uploaded_file($large_tmp_name, "$destination/$large_file_name")) { $sql .= ", large_file_name='$large_file_name' "; } } echo $sql,'<br />'; if(mysql_query($sql)) { header('Location: index.php'); } else { $status = mysql_error(); 'Unable to add your images.'; } } else { $status = '<ul>'; foreach($error_list as $error_message) { $status .= "<li>$error_message</li>"; } $status .= '</ul>'; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xlmns="http://www.w3.org/1999/xhtml" xml:lang=en-US" lang="en-US"> <head> <title>Add Image -- Administrative Area</title> <meta http-equiv="Content-Type" content="text/html"; charset=iso-8859-1" /> </head> <body> <h1>Administrative Area</h1> <h2>Add Image</h2> <h3><?php echo $type; ?></h3> <div><?php echo $status; ?></div> <form action="image_add.php" method="post" enctype="multipart/form-data"> <dl> <dt><label for="title">Title</label></dt> <dd><input type="text" name="title" id="title" value="<?php echo $title; ?>" /></dd> <dt><label for="caption">Caption</label></dt> <dd><textarea name="caption" id="caption" rows="3" cols="35"><?php echo $caption; ?></textarea></dd> <dt><label for="small">Small Image (150x180)</label></dt> <dd><input type="file" name="small" id="small" /></dd> <dt><label for="large">Large Image</label></dt> <dd><input type="file" name="large" id="large" /></dd> </dl> <div> <input type="submit" name="submit" value="Add Image" /> <input type="reset" name="reset" value="Reset Form" /> </div> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/146561-solved-trying-to-collect-data-from-the-url/#findComment-769847 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.