unable to check the query by if statement - php

I want to check if the path of the image is NULL or not but the if statement after my select query as shown below doesn't work; the code directly passes to else statement and displays the div in else statement, so anyone who can help me please...
<div class="adminLeft">
<?php
$userID=$_SESSION['userID'];
$sql="SELECT path from cim WHERE userID='$userID'";
$res=mysql_query($sql);
if ($res==NULL)
{
?>
<div class="card">
<p> <img class ="card-img-top" src="/images/picture-profile.jpg"></p>
</p>
</div>
<br>
<form id="uploadprofpic" action="up.php" enctype="multipart/form-data"
method="post">
<p> <input type="file" name="pic" accept="image/*"></p>
<input name="submit" class="formbutton" value="Upload your Photo"
type="submit" id="submitReport"/>
</form>
<?php }
else {?>
<div class="card">
<p> <img class ="card-img-top" src=""></p>
}
?> </p>
</div>

You are checking for a string "NULL", changing it to just NULL will check if it returned NULL
<?php
if ($res == NULL){
?>
<div class="card">
<p> <img class ="card-img-top" src="/images/picture-profile.jpg"></p>
<?php }?>
</div>
So now if it returns NULL it will show the div.
By the way you are not closing the php properly in the end, you have >? it is supposed to be ?>

Try is_null function like this :
if (is_null($res)){}

use simply null not "NULL"
if ($res==null){
}

Related

How to debug errors in saving images in database from different inputs

I want to save images in mysql using PHP
but I keep getting an error saying Fatal error: Call to undefined function saveimage() in D:\xampp\htdocs\PHPv2.0\Clients\subreqv2.php on line 100
And this is my code:
<body>
<form method="post" enctype="multipart/form-data">
<br/>
<!-- img1 file browser -->
<div>
<input type="file" name="image1"/>
<br/>
</div>
<!-- img2 file browser -->
<div>
<input type="file" name="image2"/>
<br/>
</div>
<!-- img3 file browser -->
<div>
<input type="file" name="image3"/>
<br/>
</div>
<!-- img4 file browser -->
<div>
<input type="file" name="image4"/>
<br/>
</div>
<!-- img5 file browser -->
<div>
<input type="file" name="image5"/>
<br/>
</div>
<!-- img6 file browser -->
<div>
<input type="file" name="image6"/>
<br/>
</div>
<!-- img7 file browser -->
<div>
<input type="file" name="image7"/>
<br/>
</div>
<input type="submit" name="sumit" value="Upload">
</form>
<?php
if(isset($_POST['sumit']))
{
if(getimagesize($_FILES['image1']['tmp_name'])== FALSE)
{
echo "Please select an image.";
}
else
{
$image1=addslashes($_FILES['image1']['tmp_name']);
$name1=addslashes($_FILES['image1']['name']);
$image1=file_get_contents($image1);
$image1=base64_encode($image1);
$image2=addslashes($_FILES['image2']['tmp_name']);
$name2=addslashes($_FILES['image2']['name']);
$image2=file_get_contents($image2);
$image2=base64_encode($image2);
$image3=addslashes($_FILES['image3']['tmp_name']);
$name3=addslashes($_FILES['image3']['name']);
$image3=file_get_contents($image3);
$image3=base64_encode($image3);
$image4=addslashes($_FILES['image4']['tmp_name']);
$name4=addslashes($_FILES['image4']['name']);
$image4=file_get_contents($image4);
$image4=base64_encode($image4);
$image5=addslashes($_FILES['image5']['tmp_name']);
$name5=addslashes($_FILES['image5']['name']);
$image5=file_get_contents($image5);
$image5=base64_encode($image5);
$image6=addslashes($_FILES['image6']['tmp_name']);
$name6=addslashes($_FILES['image6']['name']);
$image6=file_get_contents($image6);
$image6=base64_encode($image6);
$image7=addslashes($_FILES['image7']['tmp_name']);
$name7=addslashes($_FILES['image7']['name']);
$image7=file_get_contents($image7);
$image7=base64_encode($image7);
saveimage();
}
}
//displayimage();
function saveimagesaveimage($name1,$image1,$name2,$image2,$name3,$image3,$name4,$image4,$name5,$image5,$name6,$image6,$name7,$image7)
{
$con=mysql_connect("localhost","root","");
mysql_select_db("dummy",$con);
$qry="INSERT INTO images (name1,image1,name2,image2,name3,image3,name4,image4,name5,image5,name6,image6,name7,image7) VALUES ('$name1','$image1','$name2','$image2','$name3','$image3','$name4','$image4','$name5','$image5','$name6','$image6','$name7','$image7')";
$result=mysql_query($qry,$con);
if($result)
{
echo "<br/>Image successfully uploaded.";
}
else
{
echo "<br/>Error in uploading image.";
}
}
/* function displayimage()
{
$con=mysql_connect("localhost","root","");
mysql_select_db("dummy",$con);
$qry="SELECT * FROM images";
$result=mysql_query($qry,$con);
while($row = mysql_fetch_array($result))
{
echo '<img height="100" width="100" src="data:image;base64,'.$row['image'].'">';
}
}
*/
?>
</body>
Can anyone point out to me where did I go wrong?
Note: nevermind the use of mysql instead of msqli or PDO I just have to make this work so I could base my main project here.
Thanks for the help in advance. I'll appreciate it.
At the end you are calling saveimage() function, while in the code you have written the function name saveimagesaveimage().
Not only that you are passing many parameters to the function where it is initialised but where you are calling the saveimage() function even with the wrong name, no parameters are passed to it.

Printing html page and doing some action using 1 button

I have a Print button which I want to do 2 actions when clicked:
Updating my database.
Printing the HTML page.
This is what I've done so far, but it's not working:
<form action="" method="POST">
<body >
<?php
$n=$_POST['ID'];
$a=implode("</br>",$n);
list($add, $ward) = explode("(!#!)", $a);
?>
<div id="container">
<p id="address">
<?php echo"$address";?>
</p>
<p id="ward">
<?php echo"$ward";?>
</p>
</div>
<input type="submit" name="Print" value="Print" />
<?php
if(isset($_POST['Print']))
{
?><script>javascript:window.print()</script><?php
mysql_query("UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
}?>
<div id="footer">
</div>
</form>
After using this print button , my database is getting updated, but the print window is showing error(i.e the variables posted from other page are showing errors).
Can anyone please help me print and update at same time with this Print button?
If your page does not have it, add form tags. They are mandatory unless you want to AJAX the data. Also remove the semi-colon from the end of the sql
<form action="" method="POST">
<input type="submit" name="Print" value="Print" />
</form>
<?php
if(isset($_POST['Print'])) {
mysql_query("UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
?><script>window.print();</script>
<?php } ?>
UPDATE: Perhaps you mean this, but I do not want to keep correcting HTML.
<?php
$n=$_POST["ID"];
$a=implode("</br>",$n);
list($add,$ward)=explode("(!#!)", $a);
?>
<body>
<div id="headerbg">
<div id="header-e1"><a align="left" href="escalationReport.php">Back </a>
</div>
<div id="header-e3"><a align="right" href="logout.php">Logout </a>
</div>
<h1><p>Issue Notice</h1>
</div>
<div id="container">
<p id="address">
<?php echo "$address";?>
</p>
<p id="ward">
<?php echo "$ward";?>
</p>
</div>
<form action="" method="POST">
<input type="submit" name="Print" value="Print" />
</form>
<?php if(isset($_POST[ 'Print'])) {
mysql_query( "UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
?>
<script>
window.print();
</script>
<?php } ?>
<div id="footer"></div>
</body>
you have an error in the update query, you have placed semicolon inside the query so please remove that so the query will be like this.
Make sure your form method is POST
mysql_query("UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
Make sure you are using input type button inside the form tag. And use post method for form.

php function Not Displaying form

Im having a problem displaying a php function. Its for an admin log in form.
The Function Look Like this -
function displayAdmin(){
//test if login is valid
if (isset($_SESSION['adminLogin'])){
if($_SESSION ['adminLogin']=="valid"){
?>
<script type="text/javascript">location.replace('addproduct.php')</script>
<?php
}
else {
// test if login is invalid
// display error message and login form
if($_SESSION['adminLogin']=="invalid") {
echo "<div>Incorrect User ID and/or password provided</div>";
?>
<form name="adminLogin" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<div id="sign_up_form">
<label><strong>Username:</strong> <input type = "text" name="userID" class="sprited"/></label>
<label><strong>Password:</strong> <input type="password" name="passWord" class="sprited"/></label>
<div id="actions">
<a class="close form_button sprited" id="cancel" href="#">Cancel</a>
<a type ="submit" name="adminSignin"class="form_button sprited" id="log_in" href="">Sign in</a>
</div>
</div>
</form>
<?php
}
}
?>
<form name="adminLogin" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<div id="sign_up_form">
<label><strong>Username:</strong> <input type = "text" name="userID" class="sprited"/></label>
<label><strong>Password:</strong> <input type="password" name="passWord" class="sprited"/></label>
<div id="actions">
<a class="close form_button sprited" id="cancel" href="#">Cancel</a>
<a type ="submit" name="adminSignin"class="form_button sprited" id="log_in" href="">Sign in</a>
</div>
</div>
</form>
<?php
}
} // end of function
And on my page where I am wanting the function to sit the code looks like this -
<?php
session_start();
// Test that page title has been created
if (!isset($pageTitle)) {
$pageTitle = '<< Page title not set >>';
}
// include the myFunctions file
include('includes/myFunctions.php');
// test if login details have been keyed in
if(!empty($_POST["userID"])) {
// Store userID and passWord in local variables
$userID=$_POST["userID"];
$passWord=$_POST["passWord"];
// check database for valid customer
checkValidAdmin($userID, $passWord);
}
?>
and then -
<div id="sign_up">
<h3 id="see_id">Administration Log in</h3>
<span>Please sign in using the form below</span>
<div><?php displayAdmin(); ?></div>
<a id="close_x" class="close sprited" href="#">close</a>
</div>
I have searched long and hard for this problem but can not seem to find the issue, if the issue jumps out at anyone I would love to hear from you!
Thank you so much in advance!!
Definitely your $_SESSION ['adminLogin']=="valid" condition is not true. Check it is set properly or session_start(); is called at beginning of the script.
The problem is you did'nt return the html string.
You should assign the form to a variable, and at the end of the function , return this variable.
$_SESSION['adminLogin'] value is empty i.e. not set or null. that is the reason function inside condition not satisfied.

PHP: When echo a JavaScript alert, text input size decreases

I have a basic HTML form like this. When submitted, it checks for empty fields and alert the user that both fields are required. After alert, user is redirected to the same page but text input sizes decrease. How can I retain the same width, height after PHP "echo"?
Screenhot 1:
Screenshot 2:
Screenshot 3:
Controller:
function validate_user_pass() {
$this->load->library('form_validation');
$this->form_validation->set_rules('username','Email', 'required|max_length[30]');
$this->form_validation->set_rules('password','Password', 'required|max_length[32]');
if ($this->form_validation->run() == FALSE ) {
//$this->load->view('login_error');
echo '<script type="text/javascript">alert("Required fields are empty!");</script>';
$this->index();
}
else {
$this->verify();
}
}
View:
<?php echo form_open('login/validate_user_pass'); ?>
<div class="login">
<div class="login-screen">
<div class="login-icon">
<img src="<?php echo base_url(); ?>assets/images/1374606542_newsstand_ios7_ios_7.png" alt="logo">
<h4>Hotel<small>Management System</small></h4>
</div>
<div class="login-form">
<img src="<?php echo base_url(); ?>assets/ico/logo_small.png" alt="logo">
<div class="control-group">
<input type="text" class="login-field" name="username" value="<?php echo set_value('username'); ?>" placeholder="Enter username" id="login-name">
</div>
<div class="control-group">
<input type="password" class="login-field" name="password" value="" placeholder="Password" id="login-pass">
</div>
<input type="submit" class="btn btn-primary" value="Login">
</div>
</div>
</div>
<?php echo form_close(); ?>
it may be late to answer but same problem was with me. I overcome this by putting the error popup box out of the header. I searched everywhere and ended up here.
My code was something like this before:
<?php
//some code for verification and echo out/show error message
?>
<html>
<!--HTML code that has form-->
</html>
What I did was something like this:
<html>
<head>
<title></title>
</head>
<body>
<?php
//some code for verification and echo out/show error message
?>
<div>
<!--HTML code that has form-->
</div>
</body
</html>
This problem mostly exist because of echo present in header part. You can check your views for this and that may solve the problem.
This typically happens when a php error or var_dump() is on the page, but you may not see it because it is hidden under a fixed header div, for example.
Look at your View source & check if there aren't php errors on the page

how to set a controller variable in a view from a form post

I have a Zend view in PHP with a form:
<form action="https://example.com/checkout/" name="info" method="post">
<div id="payment">
<div id="paypal>
<?php $this->is_paypal_payment = true; ?>
</div>
<div id="credit_card>
<?php $this->is_paypal_payment = false; ?>
</div>
</div>
</form>
When a user clicks the submit button, how can I set the instance variable in the Controller?
This is what I tried which didn't work.
Controller:
$this->view->payment_option_paypal = false; // initialized value
$payment_method_option = $this->_getParam('payment_option_paypal', null);
if (!is_null($payment_method_option)) {
if ($payment_method_option == 'paypal') {
$this->_is_paypal = true;
}
}
View:
<div id="paypal" style="display: none;">
<?php $this->payment_option_paypal = "paypal"; ?>
<img src="paypal.gif" style="margin-right:20px;">
</div>
After clicking submit, the _getParam() function doesn't capture the string "paypal" that the view set for instance variable in Controller $this->payment_option_paypal
Any ideas what's wrong and how to go about this?
in you phtml page you have to use like this
<div id="paypal" style="display: none;">
<input type="hidden" name="paypalname" value="<?php echo $this->payment_option_paypal = "paypal"; ?>" id="paypalname"/>
<img src="paypal.gif" style="margin-right:20px;">
</div>
and the controller
function yourAction()
{
if ($this->_request->isPost())
{
$paypalvalue=$this->_getParam('paypalname');
}
}
I hope it helps you and if you have any problem in my answer then let me know.
a div will not submit data also anything that needs to be rendered as html must be echo'ed:
<form action="https://example.com/checkout/" name="info" method="post">
<div id="payment">
<div id="paypal>
<?php echo $this->is_paypal_payment = true; ?><!-- maybe should be an input element of kind and echo the $this-> statement -->
</div>
<div id="credit_card>
<?php echo $this->is_paypal_payment = false; ?><!-- maybe should be an input element of kind and echo the $this-> statement -->
</div>
</div>
</form>
again ECHO:
<div id="paypal" style="display: none;">
<?php echo $this->payment_option_paypal = "paypal"; ?>
<img src="paypal.gif" style="margin-right:20px;">
</div>

Categories