Hi, I'm trying to insert some values to database based on how many values are clicked but currently it only takes the last clicked field. I have tried hard coding with mysql_real_escape_string and the dreamweaver way. Both still give me the same last picked value instead of several values separated by a comma. Here's an example of my code: if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "worder")) { $insertSQL = sprintf("INSERT INTO tblwork_order (veh_plateno, worList, worDesc, price, expcompDate, st_username) VALUES (%s, %s, %s, %s, %s, %s)", GetSQLValueString($_POST['pno'], "text"), GetSQLValueString($_POST['pat1'], "text"), GetSQLValueString($_POST['or'], "text"), GetSQLValueString($_POST['worprice'], "text"), GetSQLValueString($_POST['sdate'], "text"), GetSQLValueString($_POST['stname'], "text")); mysql_select_db($database_localhost, $localhost); $Result1 = mysql_query($insertSQL, $localhost) or die(mysql_error()); $insertGoTo = "techdb.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo)); } <label>Parts to be worked on</label> <select multiple class="form-control" id="pat1" name="pat1[]" required> <option id="def" value="default" selected="selected">--Select--</option> do { ?> <option value="" data-price=""></option> } while ($row_services = mysql_fetch_assoc($services)); $rows = mysql_num_rows($services); if($rows > 0) { mysql_data_seek($services, 0); $row_services = mysql_fetch_assoc($services); } ?> </select> Any help to achieve this would be greatly appreciated. Thanks in advance

Recommended Answers

All 3 Replies

publish just the form
and a dump of $_POST

Do This Program In Dreamviewer and XAMPP/WAMPP Server.....

//create a database in phpmyadmin as per fields

//create a config.php file
<?php
error_reporting(0);
mysql_connect("localhost","root","");
mysql_select_db("database_name");
?>

//create a index.php file
<?php
include('config.php');
$id=$_GET['id'];
$sql=mysql_query("select * from `form` where id='$id'");
$row=mysql_fetch_assoc($sql);
?>
<form method="post" action="action.php" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>">
<table border="2" align="center">
<tr>
<td>first name:</td>
<td><input type="text" name="fname" value="<?php echo $row['fname']; ?>"></td></tr>

<tr>
<td>last name:</td>
<td><input type="text" name="lname" value="<?php echo $row['lname']; ?>"></td>
</tr>

<tr>
<td>address:</td>
<td><textarea name="address"><?php echo $row['address']; ?></textarea></td>
</tr>

<tr>
<td>email:</td>
<td> <input type="email" name="email" value="<?php echo $row['email']; ?>">
</td>
</tr>

<tr>
<td>Password:</td>
<td> <input type="password" name="password" value="<?php echo $row['password']; ?>">
</td>
</tr>

<tr>
<td>contact:</td>
<td><input type="text" name="contact" value="<?php echo $row['contact']; ?>"></td>
</tr>

<tr>
<td>gender:</td>
<td><input type="radio" name="gender" value="male"<?php if($row['gender']=='male') echo 'checked'; ?>>Male
<input type="radio" name="gender" value="female"<?php if($row['gender']=='female') echo 'checked'; ?>>Female</td>
</tr>

<tr>
<td>image:</td>
<td>
<input type="file" name="image">
<img src="image/<?php echo $row['image']; ?>" height="100" width="100" >
</td>
</tr>
<tr>
<td>city</td>
<td><select name="city">
<option><?php echo $row['city']; ?></option>
<option>amd</option>
<option>rajkot</option>
<option>vadodara</option>
</select></td>
</tr>
<tr>
<td>Language:</td>
<td>
<input type="checkbox" name="language[]">Gujarati
<input type="checkbox" name="language[]">English
<input type="checkbox" name="language[]">Hindi
</td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" name="submit" value="submit"></td>
</tr>
</table>
</form>

//create a action.php file
<?php
include('config.php');
if(isset($_POST['submit']))
{
$id=$_POST['id'];
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$add=$_POST['address'];
$email=$_POST['email'];
$pass=$_POST['password'];
$con=$_POST['contact'];
$gender=$_POST['gender'];
$image=$_FILES['image']['name'];
$tmp=$_FILES['image']['tmp_name'];
move_uploaded_file($tmp,"image/".$image);
$city=$_POST['city'];

    if($id!='')
        {
            if($image=='')
            {
                $sql=mysql_query("update `form` set `fname`='$fname',`lname`='$lname', `address`='$add', `email`='$email', `password`='$pass', `contact`='$con', `gender`='$gender', `city`='$city' where `id`='$id'");
                if($sql)
                {
                    header('location:list.php');
                }
                else
                {
                    header('location:action.php');
                }
                }

            else
            {
                $sql=mysql_query("update `form` set `fname`='$fname',`lname`='$lname', `address`='$add', `email`='$email',`password`='$pass', `contact`='$con', `gender`='$gender',`image`='$image', `city`='$city' where `id`='$id'");
            if($sql)
            {
                header('location:list.php');
            }
            else
            {
                header('location:action.php');
            }
            }
        }
    else
    {
    $sql=mysql_query("insert into `form` (`fname`,`lname`,`address`,`email`,`password`,`contact`,`gender`,`image`,`city`)
                    values ('$fname','$lname','$add','$email','$pass','$con','$gender','$image','$city')");
        if($sql)
        {
            header('location:list.php');
        }
        else
        {
            header('location:index.php');
        }
    }
}
else
{
    $id=$_GET['id'];
    $res=mysql_query("select image from form where id='$id'");
    $row=mysql_fetch_array($res);
    $sql=mysql_query("delete from form where id='$id'");
    unlink("image/".$row['image']);
    if($sql)
    {
        header('location:list.php');
    }
    else
    {
        header('location:list.php');
    }
}
?>

//create a list.php file
<?php
include('config.php');
?>
<a href="index.php"><button>Add New</button></a>
<table align="center" border="1">
<tr>
    <th>ID</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Address</th>
    <th>Email</th>
    <th>Password</th>
    <th>Contact</th>
    <th>Gender</th>
    <th>Image</th>
    <th>City</th>
    <th>Language</th>
    <th>Action</th>
</tr>
    <?php
        $i=1;
        $sql=mysql_query("select * from `form`");
        while($row=mysql_fetch_assoc($sql))
        {
    ?>
<tr>
    <td><?php echo $i++; ?></td>
    <td><?php echo $row['fname']; ?></td>
    <td><?php echo $row['lname']; ?></td>
    <td><?php echo $row['address']; ?></td>
    <td><?php echo $row['email']; ?></td>
    <td><?php echo $row['password']; ?></td>
    <td><?php echo $row['contact']; ?></td>
    <td><?php echo $row['gender']; ?></td>
    <td><img src="image/<?php echo $row['image']; ?>" height="100px" width="100px"></td>
    <td><?php echo $row['city']; ?></td>
    <td><?php echo $row['language']; ?></td>
    <td>
    <a href="index.php?id=<?php echo $row['id']; ?>"><button>Update</button></a>
    <a href="action.php?id=<?php echo $row['id'];?>"><button>Delete</button></a>
    </td>
</tr>
    <?php
    }
    ?>
</tr>
</table>

create a folder and save all this file in this folder.
and save those folder in xampp/wampp in htdocs/www folder.
then run this program in your browser.
Member Avatar for diafol

Although we value contributions, adding a solution to a dead thread and using out of date code (mysql functions) is not very helpful. Dreamweaver needs to die a horrible death too.

commented: Thanks, I didn't think of that (dying dreams.) +11
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.