Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

php - SQL query not updating database

my database is not updating and not displaying any errors, anyone ? i already set the $id and all the variables but can you please if i'm doing something wrong in my query ? thank you in advance

PHP CODE

                <?php  

                    //get values from Database for user
                    if (isset($_GET['id']) ) { 
                        $id = (int) $_GET['id']; 
                        $result = mysql_query("SELECT * FROM Credentials, Company WHERE Credentials.ID = $id AND Company.CompanyID = $id") or trigger_error(mysql_error());
                        $row = mysql_fetch_array($result);
                    }

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


                        //define index for variables 
                        if(isset($_POST['email'])){
                            $email = $_POST['email'];
                        }
                        if(isset($_POST['password'])){
                            $password = $_POST['password'];
                        }
                        if(isset($_POST['permission'])){
                            $permission = $_POST['permission'];
                        }
                        if(isset($_POST['status'])){
                            $status = $_POST['status'];
                        }


                            //safe input for all fields 
                            $email = safe_input($email);
                            $password = safe_input($password); 
                            $permission = safe_input($permission);
                            $status = safe_input($status); 






                            //double checking inputs
                            foreach($_POST AS $key => $value) { $_POST[$key] = mysql_real_escape_string($value); } 

                            //SQL queries
                            $sql = mysql_query("UPDATE Credentials SET  `Email` =  '".$email."' ,  `Password` =  '".$password."' ,  `Permission` =  '".$permission."',  `Status` =  '".$status."'  WHERE `ID` = $id ")  or die(mysql_error());

                                echo "<div id='add-success'><i class='fa fa-check'></i>&nbsp;<b>Updated Successfuly!</b></div><br/>";
                            } 

                ?>

HTML

                <form action='edit.php?id=<?php echo safe_input($id); ?>' method='POST' id="add-merchant" enctype="multipart/form-data"> 

                    <p><br />
                    <h6>Email</h6>
                    <input type='text' name='email' value="<?php echo nl2br($row['Email']); ?>" 
                            onfocus="if (this.value==this.defaultValue) this.value = ''" 
                            onblur="if (this.value=='') this.value = this.defaultValue"/> 
                    <p><br />
                    <h6>Enter New Password</h6>
                    <input type='text' name='password' value='Password'  
                            onfocus="if (this.value==this.defaultValue) this.value = ''" 
                            onblur="if (this.value=='') this.value = this.defaultValue"/> 
                    <p><br />
                    <h6>Permission</h6>
                    <select name="permission">
                        <option value="0" selected>Unverified</option>
                        <option value="1">Verified</option>
                    </select>
                    <p><br />
                    <h6>Status</h6>
                    <select name="status">
                        <option value="1" selected>Activated</option>
                        <option value="0">Deactivated</option>
                    </select>


                    <p><input type='submit' value='Update' /><input type='hidden' value='1' name='submitted' /> 

                </form> 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Is id field numeric, if so then your sql query should be like this.

$sql = mysql_query("UPDATE Credentials SET  `Email` =  '".$email."',  `Password` =  '".$password."',  `Permission` =  '".$permission."', `Status` =  '".$status."'  WHERE `ID` = $id ") // id = $id without quotes because it is numeric

IF this above query is not working try this one

mysql_query("UPDATE Credentials SET  `Email` =  '{$email}',  `Password` =  '{password}',  `Permission` =  '{$permission}', `Status` =  '{$status}'  WHERE `ID` = $id")

If this too doesn't works paste the query in phpmyadmin or your database try running the query online if possible.Also set error_reporting(E_ALL) to let browser display error;


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...