0

我正在尝试创建一个 Web 应用程序。我是 PHP 和 MySQL 的新手。当我使用 PHP 将数据插入表中时,表中会生成条目的双记录。我正在使用 XAMPP。请帮我解决这个问题。还附上了双重输入的屏幕截图。

MY Database Dump:
-- phpMyAdmin SQL Dump
-- version 3.5.2.2
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: May 18, 

2013 at 11:58 PM
 -- Server version: 5.5.27
 -- PHP Version: 5.4.7

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = 

 "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET 

@OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `gccdb`
--

-- --------------------------------------------------------

--
-- 

Table structure for table `tbl_rol`
--

CREATE TABLE IF NOT EXISTS `tbl_rol` (
 `id` int(11) NOT NULL AUTO_INCREMENT,


 `rol_cd` varchar(10) NOT NULL,
 `rol_desc` varchar(25) NOT NULL,
 `rol_shrt` varchar(15) NOT NULL,
  PRIMARY KEY (`id`)
 ) 

 ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET 

COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
 ![enter image description here][1]

Given below is the PHP code.
<?php

if ($_SERVER['REQUEST_METHOD'] == "POST") {
if ($_POST["rol_new_save"]) {
    $sql = "INSERT INTO tbl_rol(id, rol_cd, rol_desc, rol_shrt) VALUES (NULL, '$_POST[rol_new_rlcode]', 
        '$_POST[rol_new_rldesc]', '$_POST[rol_new_rlshrt]')";
    $con = mysqli_connect("localhost","root","");
        if(!$con) {
            exit('Connect Error (' . mysqli_connect_errno() . ') ' .  mysql_connect_error());
            }
        mysqli_select_db($con,"master01");
    mysqli_query($con,$sql);
    if (!mysqli_query($con, $sql)){
        die('Error:'.  mysqli_errno($con). mysqli_error($con));
    }
    mysqli_close($con);
    header('Location:rol_new.php');
    exit;
 }
 if ($_POST["rol_new_exit"]) {
    header('Location:sa_main.php');
    exit;
 }
 }
 ?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title></title>
 </head>
 <body>
    <form name="form1" action="" method="POST" enctype="multipart/form-data">
        <div>
            <p>
                <label for="rol_new_rlcode">Role Code:</label>
                <input id="rol_new_rlcode" name="rol_new_rlcode" class="" type="text"   value=""/>
            </p>
        </div>
        <div>
            <p>
                <label for="rol_new_rldesc">Role Description:</label>
                <input id="rol_new_rldesc" name="rol_new_rldesc" class="" type="text" value=""/>
            </p>
        </div>
        <div>
            <p>
                <label for="rol_new_rlshrt">Role Name:</label>
                <input id="rol_new_rlshrt" name="rol_new_rlshrt" class="" type="text" value=""/>
            </p>
        </div>
        <div id="hline"></div>
        <div>
            <p>
                <input id="rol_new_save" name="rol_new_save" class="" type="submit" value="Save"/>
            </p>
        </div>
        <div>
            <p>
                <input id="rol_new_exit" name="rol_new_exit" class="" type="submit" value="Exit"/>
            </p>
        </div>
    </form>    
</body>
</html>
4

1 回答 1

1

你正在执行它两次。把这条线拿出来;

mysqli_query($con,$sql);

你的 if 语句在评估它时执行它,所以你不需要单独调用它。

于 2013-05-18T22:16:47.113 回答