使用多个表单添加记录以在数据库表上创建一条记录

时间:2014-03-26 04:10:01

标签: php jquery mysql forms

我正在创建一个onlineshop,用户将在其中添加新记录,方法是在下拉列表中键入标题,说明,价格,图片上传和选择,其中包含要添加的产品的所有数据库表在特定的类别。

我目前使用多个表单添加新产品,所以我想知道如何使用所有表单并在用户输入标题,描述,价格(第一个表单),上传图片(第二个表单 - 使用通过单击提交按钮并收集上面列出的所有3种表格中的所有信息,选择类别,然后选择类别。

数据库的名称是'onlineshop'

的index.html

1st form(image uploading)

    <script type="text/javascript" src="../cms/scripts/jquery.min.js"></script>
<script type="text/javascript" src="../cms/scripts/jquery.form.js"></script>

<script type="text/javascript" >
$(document).ready(function() {          
    $('#photoimg').live('change', function(){ 
        $("#preview").html('');
        $("#preview").html('<img src="loader.gif" alt="Uploading"/>');
        $("#imageform").ajaxForm({
                    target: '#preview'
        }).submit();

    });
}); 
</script>

<style>

body
{
    font-family:arial;
} 

.preview
{
    width:160px;
    border:solid 2px #dedede;
    padding:10px;
} 

#preview
{
    color:#cc0000;
    font-size:10px
}

</style>
<body bgcolor="#ffdd55">    
<font face=Arial size=3 color="#880088">
<form id="imageform" method="post" action='ajaximage.php' >
<small>Upload your image <input type="file" name="photoimg" id="photoimg" /></small>
</form>
<div id='preview'>
</div>

第二种形式(文本形式)

    <form id="form1" action="insert.php" method="post" enctype="multipart/form-data">
    <div>
        <label for="title">Title: </label><input type="text" name="title"/>
    </div>
    <div>
        <label for="description">Desc: </label><input type="text" name="description"/>
    </div>
    <div>
        <label for="price">Price: </label><input type="text" name="price" />
    </div>
    <input type="submit" value="Submit">
    </form>

insert.php

    <?php session_start();

    $mysqli = new mysqli('localhost', 'root', '',"onlineshop");

    // Check connection
    if ($mysqli->connect_error)
    {
        echo "Failed to connect to MySQL: " . $mysqli->error;
    }

    $validTableNames = array('my_table_1', 'my_table_2', 'another_table_3');
    $tablename = isset($_POST['tablename']) ? $mysqli->real_escape_string($_POST['tablename']) : '';

    $tableNameOk = in_array($tablename, $validTableNames);
    if (!$tableNameOk) {
        die('Error: Invalid table name:' . $tablename);
    }

    $title       =  !empty($_POST['title'])       ? $mysqli->real_escape_string($_POST['title']) : null;
    $description =  !empty($_POST['description']) ? $mysqli->real_escape_string($_POST['description']) : null;
    $price       =  !empty($_POST['price'])       ? $mysqli->real_escape_string($_POST['price']) : null;


    $sql = "INSERT INTO {$tablename} (title, description, price) VALUES (?, ?, ?)";

    $stmt = $mysqli->prepare($sql);

    $stmt->bind_param("sss", $title, $description, $price);
    $allOk = $stmt->execute();

    if (!$allOk)
    {
        die('Error: ' . $mysqli->error);
    }
    echo "1 record added";

    $mysqli->close();

3rd form(dropdown list)

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <select id = "form3" name="Tables" id="ddTables">
<?php 

    echo $tables;

?>
    </select>
</form>

dropdown.php

<?php
 $dbname = 'onlineshop';
?>
    if (!mysql_connect('localhost', 'root', '')) {
        echo 'Could not connect to mysql';
        exit;
    }

    $sql = "SHOW TABLES FROM $dbname";
    $result = mysql_query($sql);

    if (!$result) {
        echo "DB Error, could not list tables\n";
        echo 'MySQL Error: ' . mysql_error();
        exit;
    }

    $tables = '';

    while ($row = mysql_fetch_row($result)) {

        $tables .="<option value='$row[0]'>$row[0]</option>"; 

    }

    mysql_free_result($result);
?>

connect.php

<?php
// Try to connect to MySQL
$connect = mysql_connect('localhost','root', '') or die('Sorry could not connect to database');
// Check connect and return error if failed
$use_db = mysql_select_db('onlineshop');

$create_db = "CREATE DATABASE onlineshop";
if(!$use_db) {
    echo mysql_error();
    mysql_query($create_db);
    mysql_select_db('onlineshop');
}

$con=mysqli_connect('localhost','root', '');
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

// Create database
$sql="CREATE DATABASE onlineshop";
if (mysqli_query($con,$sql))
  {
  echo "Database my_db created successfully";
  }
else
  {
  echo "Error creating database: " . mysqli_error($con);
  }

//main table
$sql = 'CREATE TABLE mens( '.
       'id INT NOT NULL AUTO_INCREMENT, '.
       'title VARCHAR(20) NOT NULL, '.
       'description  VARCHAR(45) NOT NULL, '.
       'price   FLOAT NOT NULL, '.
       'image varchar(200),'.
       'image_small varchar(200),'.
       'primary key ( id ))';

//copy attributes of the main table
$sql2= 'CREATE TABLE women AS ( SELECT * FROM mens where 1=2)';
$sql3= 'CREATE TABLE kids AS ( SELECT * FROM mens where 1=2)';
$sql4= 'CREATE TABLE infants AS ( SELECT * FROM mens where 1=2)';
$sql5= 'CREATE TABLE baby_books AS ( SELECT * FROM mens where 1=2)';
$sql6= 'CREATE TABLE garden AS ( SELECT * FROM mens where 1=2)';
$sql7= 'CREATE TABLE comics AS ( SELECT * FROM mens where 1=2)';
$sql8= 'CREATE TABLE cooking AS ( SELECT * FROM mens where 1=2)';
$sql9= 'CREATE TABLE desktop AS ( SELECT * FROM mens where 1=2)';
$sql10= 'CREATE TABLE laptop AS ( SELECT * FROM mens where 1=2)';
$sql11= 'CREATE TABLE mobile AS ( SELECT * FROM mens where 1=2)';
$sql12= 'CREATE TABLE misc AS ( SELECT * FROM mens where 1=2)';
$sql13= 'CREATE TABLE moviestv AS ( SELECT * FROM mens where 1=2)';
$sql14= 'CREATE TABLE music AS ( SELECT * FROM mens where 1=2)';
$sql15= 'CREATE TABLE games AS ( SELECT * FROM mens where 1=2)';



$retval = mysql_query( $sql, $connect );
$retval2 = mysql_query($sql2, $connect);
$retval3 = mysql_query($sql3, $connect);
$retval4 = mysql_query($sql4, $connect);
$retval5 = mysql_query($sql5, $connect);
$retval6 = mysql_query($sql6, $connect);
$retval7 = mysql_query($sql7, $connect);
$retval8 = mysql_query($sql8, $connect);
$retval9 = mysql_query($sql9, $connect);
$retval10 = mysql_query($sql10, $connect);
$retval11 = mysql_query($sql11, $connect);
$retval12 = mysql_query($sql12, $connect);
$retval13 = mysql_query($sql13, $connect);
$retval14 = mysql_query($sql14, $connect);
$retval15 = mysql_query($sql15, $connect);

//this checks only for table1, check for all of them
if(! $retval)
{
  die('Could not create table: ' . mysql_error());
}
echo "Tables created successfully\n";
?>

我是所有这些的新人,所以任何建议都会被接受。请详细解释我该怎么做。

0 个答案:

没有答案