Jump to content

Inserting values to a database table depending on the value of a drop down menu


Recommended Posts

Hello all  ;D

 

Ok here is the problem...

I want when a user inputs the requested data to the text fields , the script to insert those data in the prope table depending on the choise the user does by choosing one option from the drop down menu.

Below is the php code (apparently not working)

 

 

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
$site_type = $_REQUEST['category_selection'];
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "link_submission") && ($site_type = "Web_Sites")) {
  $insertSQL = sprintf("INSERT INTO partner_sites (url, url_title, anchor_text, `description`, webmaster_name, webmaster_email, category) VALUES (%s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['url_field'], "text"),
                       GetSQLValueString($_POST['title_field'], "text"),
                       GetSQLValueString($_POST['anchor_field'], "text"),
                       GetSQLValueString($_POST['description_field'], "text"),
                       GetSQLValueString($_POST['webmaster_nane_field'], "text"),
                       GetSQLValueString($_POST['webmaster_email_field'], "text"),
                       GetSQLValueString($_POST['category_selection'], "text"));

  mysql_select_db($database_content_conn, $content_conn);
  $Result1 = mysql_query($insertSQL, $content_conn) or die(mysql_error()); 

}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "link_submission") && ($site_type = "Blogs")) {
  $insertSQL = sprintf("INSERT INTO partner_blogs (url, url_title, anchor_text, `description`, webmaster_name, webmaster_email, category) VALUES (%s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['url_field'], "text"),
                       GetSQLValueString($_POST['title_field'], "text"),
                       GetSQLValueString($_POST['anchor_field'], "text"),
                       GetSQLValueString($_POST['description_field'], "text"),
                       GetSQLValueString($_POST['webmaster_nane_field'], "text"),
                       GetSQLValueString($_POST['webmaster_email_field'], "text"),
                       GetSQLValueString($_POST['category_selection'], "text"));

  mysql_select_db($database_content_conn, $content_conn);
  $Result1 = mysql_query($insertSQL, $content_conn) or die(mysql_error());

}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "link_submission") && ($site_type = "Directories")) {
  $insertSQL = sprintf("INSERT INTO partner_directories (url, url_title, anchor_text, `description`, webmaster_name, webmaster_email, category) VALUES (%s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['url_field'], "text"),
                       GetSQLValueString($_POST['title_field'], "text"),
                       GetSQLValueString($_POST['anchor_field'], "text"),
                       GetSQLValueString($_POST['description_field'], "text"),
                       GetSQLValueString($_POST['webmaster_nane_field'], "text"),
                       GetSQLValueString($_POST['webmaster_email_field'], "text"),
                       GetSQLValueString($_POST['category_selection'], "text"));

  mysql_select_db($database_content_conn, $content_conn);
  $Result1 = mysql_query($insertSQL, $content_conn) or die(mysql_error());
}

 

And the html form

 

<form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="link_submission" id="link_submission">

<table width="630" border="0" align="center" cellpadding="5" cellspacing="5">
  <tr>
    <td width="76">URL:*</td>
    <td width="519"><label for="url_field"></label>
      <span id="sprytextfield1">
      <label for="url_field"></label>
      <input name="url_field" type="text" id="url_field" size="50" />
      <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
    </tr>
  <tr>
    <td>Anchor Text:*</td>
    <td><label for="anchor_field"><span id="sprytextfield2">
      <input type="text" name="anchor_field" id="anchor_field" />
      <span class="textfieldRequiredMsg">A value is required.</span></span></label></td>
    </tr>
  <tr>
    <td>URL Title:*</td>
    <td><label for="title_field"><span id="sprytextfield3">
      <input type="text" name="title_field" id="title_field" />
      <span class="textfieldRequiredMsg">A value is required.</span></span></label></td>
    </tr>
  <tr>
    <td>Description:*</td>
    <td><span id="sprytextarea1">
    <label for="description_field"></label>
    <textarea name="description_field" id="description_field" cols="45" rows="3"></textarea>
    <span id="countsprytextarea1"> </span><span class="textareaRequiredMsg">A value is required.</span><span class="textareaMaxCharsMsg">Exceeded maximum number of characters.</span></span></td>
    </tr>
  <tr>
    <td>Webmaster Name:*</td>
    <td><label for="textfield2"><span id="sprytextfield4">
      <input type="text" name="webmaster_nane_field" id="webmaster_nane_field" />
      <span class="textfieldRequiredMsg">A value is required.</span></span></label></td>
    </tr>
  <tr>
    <td>Webmaster E-mail:*</td>
    <td><label for="textfield3"><span id="sprytextfield5">
    <input name="webmaster_email_field" type="text" id="webmaster_email_field" size="40" />
    <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></label></td>
    </tr>
  <tr>
    <td>Category:*</td>
    <td><span id="spryselect1">
      <label for="category_selection"></label>
      <select name="category_selection" id="category_selection">
        <option>Select An Option</option>
        <option value="Web_Sites">Web Sites</option>
        <option value="Blogs">Blogs</option>
        <option value="Directories">Directories</option>
      </select>
<span class="selectRequiredMsg">Please select an item.</span></span></td>
  </tr>
  <tr>
    <td> </td>
    <td><label for="select"></label>
      <input type="submit" name="button" id="button" value="Url Submission" /></td>
    </tr>
</table>
<input type="hidden" name="MM_insert" value="link_submission" />
</form>

 

Im begging for your help..... :D

first off, you could save yourself a lot of space by simply switch()ing on the $site_type to change the table name, and assign a single query:

 

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "link_submission"))
{
  switch($site_type)
  {
    case 'Web_sites':
      $table_name = 'partner_sites';
      break;
    case 'Blogs':
      $table_name = 'partner_blogs';
      break;
    case 'Directories':
      $table_name = 'partner_directories';
      break;
  }

    $insertSQL = sprintf("INSERT INTO `$table_name` (url, url_title, anchor_text, `description`, webmaster_name, webmaster_email, category) VALUES (%s, %s, %s, %s, %s, %s, %s)",                       GetSQLValueString($_POST['url_field'], "text"),                       GetSQLValueString($_POST['title_field'], "text"),                       GetSQLValueString($_POST['anchor_field'], "text"),                       GetSQLValueString($_POST['description_field'], "text"),                       GetSQLValueString($_POST['webmaster_nane_field'], "text"),                       GetSQLValueString($_POST['webmaster_email_field'], "text"),                       GetSQLValueString($_POST['category_selection'], "text"));
  mysql_select_db($database_content_conn, $content_conn);
  $Result1 = mysql_query($insertSQL, $content_conn) or die(mysql_error());
}

 

second, i would guess that the issue with your code is the use of $site_type. where is this variable coming from? since you're accounting for QUERY_STRING i'd assume it's coming from the URL. if so, then you still need to access it using the $_GET superglobal.

@akitchin

 

You are right, something like this was spinning to my mind also..

The $site_type , is not coming from the url , this variable is declared by me. Thought that i could "catch" the option from the drop down menu... in order to insert to the proper choosen table (category) , silly me uh?

Im prety sure something im missing here...

 

what did you name the dropdown element? should you not be using that to determine which table to insert the info to? my point was that $_POST['site_type'] might be set, but $site_type wouldn't be unless you assigned it manually.

what did you name the dropdown element? should you not be using that to determine which table to insert the info to? my point was that $_POST['site_type'] might be set, but $site_type wouldn't be unless you assigned it manually.

The drop down menu is named "link_submission" .

Thing is that i can not find a way to say to the drop dowm menu 'send the choosen option for switch()ing' , for the proper insertion to the database to take place. I think now you got my way of thinking.

my mistake - I should have looked at the HTML form. since your dropdown select box is named "category_selection", you should be able to use:

 

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "link_submission"))
{
  switch($_POST['category_selection'])
  {
    case 'Web_sites':
      $table_name = 'partner_sites';
      break;
    case 'Blogs':
      $table_name = 'partner_blogs';
      break;
    case 'Directories':
      $table_name = 'partner_directories';
      break;
  }
  $insertSQL = sprintf("INSERT INTO `$table_name` (url, url_title, anchor_text, `description`, webmaster_name, webmaster_email, category) VALUES (%s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['url_field'], "text"),
                       GetSQLValueString($_POST['title_field'], "text"),
                       GetSQLValueString($_POST['anchor_field'], "text"),
                       GetSQLValueString($_POST['description_field'], "text"),
                       GetSQLValueString($_POST['webmaster_nane_field'], "text"),
                       GetSQLValueString($_POST['webmaster_email_field'], "text"),
                       GetSQLValueString($_POST['category_selection'], "text"));
  mysql_select_db($database_content_conn, $content_conn);
  $Result1 = mysql_query($insertSQL, $content_conn) or die(mysql_error());

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

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