doumor942473 2018-01-01 10:56
浏览 35

订阅按钮不会向数据库发送信息[重复]

This question already has an answer here:

I have a problem with the php file below.When I click Send button, it shows a new window that says that the connection is ok and informations added to the database, but when I check the database, neither name nor email have been added. Will you please help me out to understand what's the problem?

<html>

<body>
<?php


$name = filter_input(INPUT_POST, 'name');
$email = filter_input(INPUT_POST, 'email');

    $servername = "127.0.0.1";
    $username = "username";
    $password = "password";
    $dbname = "Subscription";
    $con = mysql_connect($servername,$username,$password,$dbname);
    if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }


    echo "Connected successfully";
    mysql_select_db("Subscription", $con);
    $name = false;
    if (isset($_POST['name'])) { $name = $_POST['name'];

    $email = false;
    if (isset($_POST['email'])) { $name = $_POST['email'];
 }
}

    echo "'You have been successfully added.' . '<br>'; ";

mysql_close($con)

?>
</body>

</html>

this is the code I used after the suggested corrections:

$con = mysql_connect($servername,$username,$password,$dbname);
    if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }


    echo "Connected successfully";
      mysql_select_db("Subscription", $con);

    $sql =  
$sql = "INSERT INTO newsletter (name, email) VALUES ('".mysql_real_escape_string($_POST['name'])."','".mysql_real_escape_string($_POST['email'])."');";
         if (!mysql_query($sql,$con))

          {

          die('Error: ' . mysql_error());

          }

        echo "'You have been successfully added.' . '<br>'; "

mysql_close($con);

?>

it gives me this error now: Parse error: syntax error, unexpected 'mysql_close' (T_STRING), expecting ',' or ';' in /opt/lampp/htdocs/project/Newsletter-signup.php on line 38.

Thanks for your help Costantin, my head is exploding.

</div>
  • 写回答

2条回答 默认 最新

  • dongyan5239 2018-01-01 11:27
    关注

    i have xampp on windows with mariadb and mysqli oop so i can't send you a complet script to test but i think you should try this

    $sql = "INSERT INTO newsletter (name, email)
      VALUES ('".$_POST[name]."', '".$_POST[email]."')";
    

    or to try this

    $sql = "INSERT INTO newsletter (name, email)
      VALUES ('{$_POST[name]}', '{$_POST[email]}')";
    

    these "rooles" ,by experience, you shouldn't avoid in php : 1.you should use {,} more than you expect inside "", 2.you should avoid sending content before a php script part should do something(so the next time you need to add headers or write cookies,sessions,etc.. you will not force to set it in the top of the file and all your code will be compact and easy to view/edit

    eg. i use one include file (normal php file:biggest file 2090 lines=63kbytes or minified php file with modules : 3 lines =37 kbytes) and a page for every view in the application/website with a template page as well so you need to centralize and focus all efficiency around a modules you will use in all others pages.

    i use oop =i am forced cause xampp put mariadb with php 7 to get the max speed and quality of php execution and i had mysqli driver: i could insert my own function to make conversion mysqli -> mysql user fuctions but i don't need a delay caused by back conversions oop -> structural,so all my code inside modules is oop.

    your script should be modified also like this

    <?php
    
    //here avoid using of output content or a space before <?php
    $name = filter_input(INPUT_POST, 'name');
    $email = filter_input(INPUT_POST, 'email');
    //other stuff
    
    //mysql stuff
    
    
    
    ?><html>
    
    <body>
    <?php
    //output here
    
    ?>
    </body>
    
    </html>
    

    in the past i used cobinations of this 'cause how that guy said mysql is depreceated !

    function insert($table){//insert(table,'var1=$val1',"var2=$val2")
    
        $columns = mysql_query("SHOW COLUMNS FROM "._T($table));
        while ($row = mysql_fetch_assoc($columns)) {
            if((strpos($row['Type'],'char')!==false)or(strpos($row['Type'],'blob')!==false))
                $schema[strtolower($row['Field'])]=true;
            else
                $schema[strtolower($row['Field'])]=false;
        }//$schema
    
        $na = func_num_args();
        if($na==0 or strpos(func_get_arg(0),'=') !== false)
            die('the table '.$table.'its missing !!!');
        if($na>=2){
            for($k=1;$k<$na;$k++){
                $param_i=func_get_arg($k);
                if(strpos($param_i,'=') !== false){// include all "var=values" for overwrite        
                    $temp_para=explode('=', $param_i );
                    $params[]=$temp_para[0];
                    $values[]=$temp_para[1];
                    $matrix[$k]=$schema[$temp_para[0]];//matrix
                }
            }
        }
        $string="INSERT INTO `".$table.'` (';
        $la=count($params);
        $x=1;
        foreach($params as $ch => $vl){
            if($x!=$la)
                $string.='`'.$vl.'`,';
            else
                $string.='`'.$vl.'`';
            $x++;
        }
        $string.=')VALUES (';
    
        $x=1;
        foreach($values as $ch => $vl){
            if($matrix[$x])
                $tpl="'";
            else
                $tpl="";
            if($x!=$la)
                $string.=$tpl.$vl.$tpl.',';
            else
                $string.=$tpl.$vl.$tpl;
            $x++;
        }
        $string.=');';
        //die
        //print
        //  ($string);
    mysql_query($string);
    return mysql_insert_id();
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口