All pastes #257811 Raw Edit

Daveyboy

public text v1 · immutable
#257811 ·published 2006-11-25 20:16 UTC
rendered paste body
<?php #script 12.5 add_url.php

$page_title= 'Add a Url';
include ('./includes/header.html');
require_once('../mysql_connect.php');

//check if the form is submitted

if (isset($POST['submitted'])){

//validate a submittted url
if (eregi('^([[:alum:]\-\.])+(\.)([[:allum:]]){2,4}([[:allum:]/+=%&_\.~?\-]*)$', $_POST['url'])){
    $u = escape_data($_POST['url']);
}else{
	$u = FALSE;
	echo '<p><font color="red">Please enter a valid url!</font></p>';
}

//validate the URL's title and description

if (!empty($_POST['title'])){
  $t= escape_data($_POST['title']);
}else{
  $t = FALSE;
  echo '<p><font color="red">Please enter a url name/title!</font></p>';
  }

//check for a description
if (!empty($_POST['description'])){
  $d = escape_data($_POST['description']);
}else{
   $d= FALSE;
  echo'<p><font color="red">Please enter a description!</font></p>';
}

//check that the category has been selected

if (isset($_POST['types']) && (is_array($_POST['types']))){
  $type = TRUE;
}else{
  $type = FALSE;
  echo '<p><font color="red">Please select a category!</font></p>';
}

//if all is okay, add the url to the url table

if ($u && $t && $d && $type){
  $query = "INSERT INTO urls (url,title, description) VALUES ('$u', '$t', '$d')";
$result = myssql_query($query);
$uid =mysql_insert_id();
}

//Build the url_associations INSERT query.

if ($uid > 0){
  $query= 'INSERT INTO url_associations (url_id, url_category_id, approved)VALUES';
  foreach ($_POST['types'] as $v){
 $query .="($uid, $v, 'Y'),";   
}
$query= substr ($query, 0, -2);

//run the second query and report the results

$result = @mysql_quesry ($query);
if (mysql_affected_rows()== count($_POST['types'])){
  echo '<p><b>Thank You for your submission!</b></p>';
  $_POST= array();
}else{
  echo '<p><font color="red"> Your submission could not be processed due to a system error. We apologize for any inconvenience</font></p>';
  echo '<p><font color="red">'.mysql_error(). '<br/><br/> Query:'. $query .'</font></p>';
  
$query= "DELETE FROM urls WHERE url_id=$uid";
@mysql_query ($query);
}

//Complete the remaining two conditionals
}else{
  echo '<p><font color="red">Your submission could not be processed due to a system error. We apologize for any inconvenience.</font> </p>';
  echo '<font color="red">' .mysql_error() . '<br /><br />Query: ' . $query . '</font></p>';
}
}else{
  echo'<p><font color="red"> Please try again. </font></p>';
}

?>

<form action="add_url.php" method="post">
<fieldset><legend>Fill out the form to submit a URL:</legend>
<p><b>URL: </b><input type="text" name="url" size="60" maxlength="60" value= "<?php if(isset($_POST['url']))
echo $_POST ['url'];?>" /><br /><small>Do NOT include the initial <i>http:</i>.</small></p>

<p><b>URL Name/Title:</b> <input type="text" name="title" size="60" maxlength="60" value= "<?php if(isset($_POST['title']))
echo $_POST ['title'];?>" /><br />

<p><b>Description:</b> <textarea name="description" cols="40" rows="5"> <?php if(isset($_POST['description']))
echo $_POST ['description'];?> </textarea></p>

<!--create the categories select box-->

<p><b>Category/Categories:</b>
<select name= "types[]" multiple= "multiple" size= "5">
<?php
$query= "SELECT * FROM url_categories ORDER BY category ASC";
$result= mysql_query($query);
while ($row = mysql_fetch_array($result, MYSQL_NUM)){
  echo "option value=\"$row[0]\"";
  if(isset($_POST['types']) && (in_array($row[0], $_POST['types']))){
    echo 'selected="selected"';
  }
echo ">$row[1]</option>\n";
}
?>
</select></p>

<!--complete the form and page-->

</fieldset>
<input type="hidden" name="submitted" value="TRUE" />
<div align="center"><input type="submit" name="sumbit" value="Submit" /></div>
</form>

<?php
mysql_close();
include('./includes/footer.html');
?>