Jump to content

Recommended Posts

Hi All,

 

I have a PHP form that submits data to the database table. Everything is working however, for the purpose of having a better structure in the table I would like to make a change.

 

On one part of the form the user would select a Visit Reason. If the user selects Sale they are given the "Primary" sales reasons from a dynamic drop-down box.

 

As a sale can contain more than one type of sale e.g handset/accessory I have also added additional tick boxes to add any additional items that may for part of that sale. Currently the "Primary" sale reason is writing to one column in my table, and then each of the tick boxes will also write to their own individual boxes. What this means is Accessory can be written as the primary reason, and then again in its own field in the table.

 

How can I get each of the fields from the drop-down to write to their individual table fields rather than have an additional field?

Also, is it able to activate the radio buttons only if sale is selected from the drop-down?

 

Here is submit code below:

 

<?php$con = mysql_connect("localhost","username","password");

if (!$con)
{die('Could not connect: ' . mysql_error());
}
mysql_select_db("store", $con);

$sql="INSERT INTO store_traffic
(username
, gender
, ethnic_group
, age_group
, requirement
, visit_detail
, accessory
, airtime
, handset
, postpaid
, prepaid
, handset_make)

VALUES
('$_POST[username]'
,'$_POST[gender]'
,'$_POST[ethnic_group]'
,'$_POST[age_group]'
,'$_POST[requirement]'
,'$_POST[visit_detail]'
,'$_POST[accessory]'
,'$_POST[airtime]'
,'$_POST[handset]'
,'$_POST[postpaid]'
,'$_POST[prepaid]'
,'$_POST[handset_make]')";

if (!mysql_query($sql,$con))
{die('Error: ' . mysql_error());
}
echo "<b><font color='white' face='segoe' size='2'>1 record added</b></font>";
include "redirect_store.html";mysql_close($con)?>

If I'm reading it correctly, try using update rather than insert.

 

Update, updates (obviously) existing entries, whereas insert creates a new row for each entry.

 

 

Also, since functions such as select, insert, update, delete, etc are very common functions to use in php - can I suggest that you write custom functions for them?

 

Selecting:

//Select
function select($row = "*", $table, $where = NULL, $order = NULL, $limit = NULL)
{
$query = "SELECT ".$row." FROM ".$table;

if($where != NULL){
$query .= " WHERE ".$where;
}if($order != NULL){
$query .= " ORDER BY ".$order;
}if($limit != NULL){
$query .= " LIMIT ".$limit;
}

$result = mysql_query($query);

return $result;
}

 

Calling the function:

$welcome = select("*", "welcome");
$rowWelcome = mysql_fetch_assoc($welcome);

echo $rowWelcome["text"];

Edited by White_Lily

I'm not sure how to explain it, but its not an update function it a new entry being written to the database each time the user clicks submit.

 

Maybe this table will explain it better?

 

 

Ok the table didn't work, so I had to remove the comment. Let me send you an attachment

Edited by SalientAnimal

I can easily delete it, I'm just not sure how to have the data written to those other columns from the submit script, rather writing to the visit_detail column.

 

The drop-down needs to be there, as it defines a whole lot of dynamic drop-downs, but the check boxes are also required, as there might be additional reasons for the visit.

Mmmm... I don't think I'm explaining it properly, but also don't know how to. And then again I also don't want to go and type a long message that no one will read trying to explain it. I can maybe try do a flow diagram in word/excel if that will help explain it?

Why do you have visit_detail at all if it is just a duplicate of the other columns?  Why not just remove the column all together and if that field is required, require them to check at least one of the other boxes?

 

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.