如何将变量转换为子句

时间:2012-02-29 10:21:37

标签: php variables boolean colorbox clause

我有以下问题:我有一个注册表。响应消息将通过javascript函数(colorbox)显示,该函数将通过php变量调用。

看起来像:

<?php if ($result):?>
  <script type="text/javascript">no need to post, it is working fine</script>
<?php endif; ?>

该变量无法正常工作。如果我设置:

<?php if **(isset($_POST['submitted']))**: ?>
  <script type="text/javascript"> no need to post it is working</script>
<?php endif; ?>

它工作正常。

问题是,当我要提交表格时。即使未正确填写注册表单,该框也会启动。为了避免这个问题,我想我必须找到一个类似布尔值true或false的变量,如果该表单以正确的方式填充哪个答案来自我的服务器。我的想法就像:

$sql = mysql_query("INSERT INTO tabelle (x1, x2...) VALUES('$x1','$x2')") 
       or die (mysql_error()); 

if ($result = mysql_affected_rows() = 2) {
  return TRUE;
} 

$result = mysql_affected_rows(); 
$result = mysql_query($sql);

通过这种方式,我可以将变量$result更改为应该有效的子句。它没有。 :(

我做错了什么?

3 个答案:

答案 0 :(得分:1)

在您的代码中

$sql = mysql_query("INSERT INTO tabelle (x1, x2...) VALUES('$x1','$x2')") 
       or die (mysql_error()); 

if ($result = mysql_affected_rows() = 2) {
  return TRUE;
} 

$result = mysql_affected_rows(); 
$result = mysql_query($sql);

为什么你回来了?如果这样做,它将无法到达后来的行。

此外,您在mysql_query($sql)中使用$ sql,它已经是一个mysql结果资源。

如果您想在查询成功时显示

<?php
  ..
   $sql = mysql_query("INSERT INTO tabelle (x1, x2...) VALUES('$x1','$x2')") 
       or die (mysql_error()); 

if (mysql_affected_rows() == 2){ ?>
  <script type="text/javascript">no need to post, it is working fine</script>

<?php } ?>

答案 1 :(得分:0)

这段代码有很多错误。

这样做:

<?PHP
$success = false;
$result = mysql_query("INSERT INTO tabelle (x1, x2...) VALUES('$x1','$x2')") or die (mysql_error()); 
if ($affected = mysql_affected_rows() == 2) { $success = true; }
?>
<?PHP if ($success){ ?>
<script type="text/javascript">no need to post, it is working fine</script>
<?PHP } ?>

很抱歉,如果不能完全奏效,你会对我('_ POST ['提交'])部分感到困惑,你的问题并不完全清楚。

答案 2 :(得分:0)

只是为了显示整个代码。以下所有查询都有效。

    <?php

$errorMsg = "";
$x1 = "";
$x2 = "";
$x3 = "";
$x4 = "";
$email = "";
$password = "";
$x7= "";

if (isset ($_POST['x1'])){

     $x1 = $_POST['x1'];
     $x2 = $_POST['x2'];
     $x3 = $_POST['x3'];
     $x4 = $_POST['x4'];
     $email = $_POST['email'];
     $password = $_POST['password'];
     $x7= $_POST['x7'];

         $x1 = stripslashes($x1);
     $x2 = stripslashes($x2);
         $x3 = stripslashes($x3);
     $x4 = stripslashes($x4);
     $email = stripslashes($email);
     $password = stripslashes($password);
     $x7= stripslashes($x7);

         $x1 = strip_tags($x1);
     $x2 = strip_tags($x2);
     $x3 = strip_tags($x3);
     $x4 = strip_tags($x4);
     $email = strip_tags($email);
     $password = strip_tags($password);
     $x7= strip_tags($x7);

     include_once "scripts/connect_to_mysql.php";
     $emailCHecker = mysql_real_escape_string($email);
     $emailCHecker = eregi_replace("`", "", $emailCHecker); 

     $sql_email_check = mysql_query("SELECT email FROM tablename WHERE email='$emailCHecker'"); 

     $email_check = mysql_num_rows($sql_email_check); 

     if ((!$x1) || (!$x2) || (!$x3) || (!$x4) || (!$email) || (!$password) || (!$x7)) { 

     $errorMsg = 'following issues are missing<br />';

    if(!$x1){ 
       $errorMsg .= ' *x1<br />'; 
     } 
     if(!$x2){ 
       $errorMsg .= ' *x2<br />';
     } 
     if(!$x3){ 
       $errorMsg .= ' *x3<br />';
     } 
     if(!$x4){ 
       $errorMsg .= ' *x4<br />'; 
     } 
     if(!$email){ 
       $errorMsg .= ' *email<br />'; 
     }
     if(!$x7){ 
       $errorMsg .= ' *x7<br />'; 
     }  

     } else if ($email_check > 0){ 
       $errorMsg = "Mail already exists"; 

     } else 

     { 

     $x1 = mysql_real_escape_string($x1);
     $x2 = mysql_real_escape_string($x2);
     $x3 = mysql_real_escape_string($x3);
         $x4 = mysql_real_escape_string($x4);
     $email = mysql_real_escape_string($email);
     $password = mysql_real_escape_string($password);

     $x1 = eregi_replace("`", "", $x1);
     $x2 = eregi_replace("`", "", $x2);
     $x3 = eregi_replace("`", "", $x3);
     $x4 = eregi_replace("`", "", $x4);
     $email = eregi_replace("`", "", $email);

     $db_password = md5($password); 

     $sql = mysql_query("INSERT INTO tablename (x1, x2, x3, x4, sign_up_date, email, password)    

     VALUES('$x1','$x2','$x3','$x4', now(), '$email','$db_password')")  

     or die (mysql_error()); 

     $id = mysql_insert_id();

     /*mkdir("members/$id", 0755); */

    $to = "$email"; 

    $from = "xxx@xxx.xx";       
    $subject = "activationlink";        

    $message = "Link for activation";

    $headers  = "From: $from\r\n";
    $headers .= "Content-type: text\r\n";

    mail($to, $subject, $message, $headers);

   $msgToUser = "text";


   /*include_once 'msgToUser.php';  */



   exit();

   } 

} else { 

      $errorMsg = "Errormessages";
          $x1 = "";
      $x2 = "";
      $x3 = "";
      $x4 = "";
      $email = "";
      $password = "";
      $x7 = "";


}

?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>XXXX</title>

        <link rel="stylesheet" href="css/colorbox.css" />
        <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script> 
        <script type="text/javascript" src="js/jquery.colorbox.js"></script>   

<?php if **a variable or clause will be true: ?>
        <script type="text/javascript">
        $(document).ready(function(){
        $.colorbox({iframe:true, scrolling:false, href:"#$msgToUser.php", innerWidth:"408px", innerHeight:"292px", opacity:0.75, overlayClose:false, escKey:false, });
        });
        $("#colorboxCloseBtn").click(function() {
        $.colorbox.close();
        });
        $(document).ready(function() {
        /* Automatically resize to content 
        var y = $(document.body).height();
        var x = $(document).width();
        parent.$.colorbox.resize({innerWidth:x, innerHeight:y});*/

        $("#button_cancel").click(function() {
        parent.$.colorbox.close();
        return false;
        })
        });
        </script> 
 <?php endif; ?>