English 中文(简体)
PHP 插入到 MSSQL 窗体 [已关闭]
原标题:PHP insert into MYSQL form [closed]
  • 时间:2012-05-23 17:39:44
  •  标签:
  • php
  • mysql

我的第一个php项目有问题, 我这样做是为了自我教育一下。 所以我做了一个表格在这里是代码:

< 加强> vpis.html

<html>
    <head>
        <title>Php-Web Page - Input</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <div id="glava">
            <center>Php Spletna Stran</center>
            <hr/>
        </div>

        <div id="menu">
            <center>
                <a href="index.php">NAZAJ</a>
                <a href="bris.html">BRISANJE</a>
            </center>
            <hr/>
        </div>

        <div id ="vsebina">

            <div div="vpisigr">
                <form action="insert.php" method="post">
                    Vpisovanje igralca<br/>
                    ID:<input type="text" name="pid" size="20"/><br/>
                    Ime:<input type="text" name="pime" size="20"/><br/>
                    Priimek:<input type="text" name="ppriimek" size="20"/><br/>
                    Starost:<input type="text" name="pstarost" size="20"/><br/>
                    Velikost:<input type="text" name="pvelikost" size="20"/><br/>
                    Polozaj:<input type="text" name="ppolozaj" size="20"/><br/>
                    ID Kluba:<input type="text" name="ppid" size="20"/><br/>
                    <input type="submit" value="Shrani"/>
                    <input type="reset" value="Reset"/>
                </form>
            </div>
        </div>

        <hr/>

        <div id="footer">
            <center>Vse pravice pridržane </center>
        </div>
    </body>
<html>

所以这里我要求用户输入所有数据插入数据库。 这是数据库的屏幕图集 :

"https://i.sstatic.net/HbMQP.png" alt="数据库图像"/>

此处是“ 加固” 插入.php

<?php
header("Content-type: text/html; charset=utf-8");
$con = mysql_connect("localhost","username","pass");
if (!$con)
  {
  die( Could not connect:   . mysql_error());
  }

mysql_select_db("php", $con);
//hmmmm ne dela
$sql="INSERT INTO Igralec (iID, iIME, iPRIIMEK, iSTAROST, iVELIKOST, iPOLOZAJ, ID)
VALUES ( $_POST[pid] ,  $_POST[pime] ,  $_POST[ppriimek] ,  $_POST[pstarost] ,  $_POST[pvelikost] ,  $_POST[ppolozaj] ,  $_POST[ppid] )";

if (!mysql_query($sql,$con))
  {
  die( Error:   . mysql_error());
  }

//die(print_r($_POST));
echo "Dodana je ena nova vrstica";


mysql_close($con);
?> 

The problem is thath evry time i submit data, it writes false data into database all in format like 0--0-0-00 or simmilar to thath. But in array before is written it contains correct data but somehow it writes in database false data.

最佳回答

尝试将您的 $sql 变量编辑为此 :

    $sql="INSERT INTO Igralec (iID, iIME, iPRIIMEK, iSTAROST, iVELIKOST, iPOLOZAJ, ID)
VALUES ( ".$_POST[pid]." ,  ".$_POST[pime]." ,  ".$_POST[ppriimek]." ,  ".$_POST[pstarost]." ,  ".$_POST[pvelikost]." ,  ".$_POST[ppolozaj]." ,  ".$_POST[ppid]." )";
问题回答

尝试此选项作为您的查询 :

$sql="INSERT INTO Igralec (iID, iIME, iPRIIMEK, iSTAROST, iVELIKOST, iPOLOZAJ, ID)
VALUES ( ".$_POST[ pid ]." ,  ".$_POST[ pime ]." ,  ".$_POST[ ppriimek ]." ,  ".$_POST[ pstarost ]." ,  ".$_POST[ pvelikost ]." ,  ".$_POST[ ppolozaj ]." ,  ".$_POST[ ppid ]." )";

当处理关联阵列时, 键像字符串一样 。

$_POST[ ppolozaj ]

你应该把你的变量放在卷轴牙套里 以便更容易解析

$sql="INSERT INTO Igralec (iID, iIME, iPRIIMEK, iSTAROST, iVELIKOST, iPOLOZAJ, ID)
VALUES ( {$_POST[ pid ]} ,  {$_POST[ pime ]} ,  {$_POST[ ppriimek ]} ,  {$_POST[ pstarost ]} ,  {$_POST[ pvelikost ]} ,  {$_POST[ ppolozaj ]} ,  {$_POST[ ppid ]} )";

我理解这是为学习目的。最好在以后学习最佳做法。本发言中有助于安全的两个方面是: 功能和http://php.net/manual/en/formation.mysql- real-escape-string.php" rel=“nofolpol” >mysql_real_escape_string 功能。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签