I'm working on a classifieds I'm setting up for my site and I've found myself "Stuck" again.
I'm new to PHP but I'm working on developing a better mouse trap for the classifieds and the script I will provide below I have written entirely from scratch and it works. (uploads data to the DB) but when I add the if block starting with: if($_POST['Image 1'] != "") { the whole thing crashes. (I get a white screen with no error messages or anything).
Here's the full code for this page;
<?php
ob_start();
session_start();
require_once('z-mysql_ini.php');
require ('z-connect.php');
$mysql = new mysql();
if($_POST['Description'] != "") {
$TypeID = $_POST['TypeID'];
$FatherID = $_POST['fatherID'];
$Model = $_POST['Model'];
$Price = $_POST['Price'];
$Description = $_POST['Description'];
$Year = $_POST['Year'];
$VIN = $_POST['VIN'];
$Address = $_POST['Address'];
$City = $_POST['City'];
$State = $_POST['State'];
$ZIP = $_POST['ZIP'];
$Country = $_POST['Country'];
$Email = $_POST['Email'];
$Phone = $_POST['Phone'];
$InformOfUpdates = $_POST['Inform'];
}
$node = new sqlNode();
$node->table = sprintf("`tt_%s`", abs(intval($_POST['TypeID'])));
$node->push("int", "CategoryID", $FatherID);
$node->push("int", "MemberID", $_SESSION['memberID']);
$node->push("text", "Model", strtoupper($Model));
$node->push("int", "Price ($)", $Price);
$node->push("text", "Description", $Description);
$node->push("int", "Year", $Year);
$node->push("text", "VIN", $VIN);
$node->push("defined","PostDate","NOW()");
$node->push("text","Featured","yes");
$node->push("defined","ExpireDate","DATE_ADD(NOW(),INTERVAL 30 DAY)");
$node->push("text", "Address", $Address);
$node->push("text", "City", $City);
$node->push("text", "State/Province", $State);
$node->push("text", "ZIP/Postal", $ZIP);
$node->push("text", "Country", $Country);
//$node->push("text", "Email", $Email);
//$node->push("text", "Phone", $Phone);
$node->push("int", "Inform", $InformOfUpdates);
if(($result = $mysql->insert($node)) === false) {
echo mysql_error();
echo 'Unable to Insert Data';
} else {
$ListingID = mysql_insert_id();
}
if($_POST['Image 1'] != "") {
echo 'File Selected and Data set in DB';
} else{
echo 'No File Selcted but Data Inserted';
//header("Location:memberindex.php");
}
}
?>
If anyone could kindly show me what I'm doing wrong that's making the page crash, I'd be most appreciative as then I can move forward with re-implementing the image upload code that came with the classifieds, but I can't even get past this initial block of test code without the page dying (and not uploading to the DB.
Bottom line is if I take the following code off of the page entirely, the script will upload all of the variable data above into the DB but if I add this block of code the whole thing crashes.
if($_POST['Image 1'] != "") {
echo 'File Selected and Data set in DB';
} else{
echo 'No File Selcted but Data Inserted';
//header("Location:memberindex.php");
}
Related
I've been trying to make a payment page where after adding sufficient information, the order is taken with the items in the cart. for some reason, my code isn't responding and is showing a blank screen. I am relatively new to PHP. could anyone help me out? Here is the code below:-
<?php
require_once('../connection.php');
require_once('../product.php');
session_start();
$name = $_POST['name'];
$email = $_POST['email'];
$address = $_POST['address'];
if(isset($_POST['payment-made'])){
if(empty($name) || empty($email) || empty($address)){
echo "<script>alert('Please enter the complete information!');
window.location = 'orders.inc.php';</script>";
} else {
if(isset($_SESSION['Uid'])){
if(isset($_SESSION['cart'])){
$product_id = array_column($_SESSION['cart'], 'product_id');
$result = mysqli_query($conn,"SELECT * FROM `products`");
while($row = mysqli_fetch_assoc($result)){
foreach($product_id as $id){
if($row['product_id']==$id){
$ordername = $row['item_name'];
$nooforder = 1;
$sql1 = "INSERT INTO orders(order_name, no_order, cust_name, del_add)
VALUES ('$ordername', '$nooforder', '$name', '$email', '$address');";
$done = mysqli_query($conn, $sql1);
if($done){
echo "successful";
}
else{
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
}
}
}
}
}
}
}
}
?>
I have doubts the problem lies within the $ordername variable. Could anyone clarify what's causing the problem and how to solve it?
Your INSERT query names four columns, but provides five variables to insert.
My code is below it is running on the localhost successfully but not running on hosting server. i tried many time to resolve but i am not able to solve it
<?php
include ('dbconfig.php');
$page = isset($_GET['p'])?$_GET['p']:'';
if($page =='add'){
$email_id = $_POST['email_id'];
$customer_name = $_POST['customer_name'];
$mobile_no = $_POST['mobile_no'];
$address = $_POST['adress'];
$city = $_POST['city'];
$state = $_POST['state'];
$country = $_POST['country'];
$no_of_jurney_person = $_POST['no_of_jurney_person'];
$j_from = $_POST['j_from'];
$j_to = $_POST['j_to'];
$date_of_jurney = $_POST['date_of_jurney'];
$return_date = $_POST['return_date'];
$stmt=$DBcon->prepare("insert into anil values('',?,?,?,?,?,?,?,?,?,?,?,?)");
$stmt->bindParam(1,$email_id);
$stmt->bindParam(2,$customer_name);
$stmt->bindParam(3,$mobile_no);
$stmt->bindParam(4,$address);
$stmt->bindParam(5,$city);
$stmt->bindParam(6,$state);
$stmt->bindParam(7,$country);
$stmt->bindParam(8,$no_of_jurney_person);
$stmt->bindParam(9,$j_from);
$stmt->bindParam(10,$j_to);
$stmt->bindParam(11,$date_of_jurney);
$stmt->bindParam(12,$return_date);
if($stmt->execute()){
echo "Mr. Or Mrs ".$customer_name;
echo "Thanks for inquiry. Our representative will call you soon";
}
else{
echo "Something went wrong. Please try again later";
}
I am adding an article to database, this code worked fine all these days but suddenly stopped working (didn't messed anything) and I cant find any problem with this code but when ever Ii post something it's returning me failed.
<?php
if (!isset($_POST['submit']))
{
echo 'hmm';
}
else
{
//$_POST['title'] = filter_var($_POST['title'], FILTER_SANITIZE_STRING);
$article_title = $_POST['title'];
$article_date = date('d-m-y');
$article_author = $_POST['author'];
$article_keywords = $_POST['keywords'];
//$_POST['content'] = filter_var($_POST['content'], FILTER_SANITIZE_STRING);
$article_content = $_POST['content'];
$article_category = $_POST['category'];
$article_image = $_FILES['image']['name'];
$image_tmp = $_FILES['image']['tmp_name'];
if ($article_title == '')
{
echo "<script> alert('enter a title')</script>";
exit();
}
else
{
move_uploaded_file($image_tmp, "images/$article_image");
$add = "insert into articles(article_title,article_author,article_date,article_keywords,category,article_content,article_image) values ('$article_title','$article_author','$article_date','$article_keywords',$article_category,'$article_content','$article_image')";
if (mysqli_query($conn, $add) == 1)
{
echo "success";
}
else
{
echo "failed";
}
}
}
?>
EDIT : i am getting an error saying i am getting failedChamp 'urdfhghduk'(category input) inconnu dans field list
Hello everyone i'm able to display my record by passing an id by query string to another page, but i'm not able to update it, the problem is that when i click on update nothing happen, it return me a blank page, and there is no printed error, can someone help me please?
<?php
require 'db2.php';
$id = null;
if ( !empty($_GET['id'])) {
$id = $_REQUEST['id'];
$dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );
$q = mysqli_query($dbc,"SELECT * FROM movie WHERE MovieID = '$id' ");
while($r=mysqli_fetch_array($q))
{
$title = $r["Title"];
$tag = $r["Tag"];
$year = $r["YEAR"];
$cast = $r["Cast"];
$comment = $r["Comment"];
$IDBM = $r["IMDB"];
}
}
At this stage, the code display every information i need , the stage below is where i'm having a problem, i'm not able to get the id against and make the update when click on update button
elseif (!empty($_POST) and !empty($_GET['id']) ) {
// keep track post values
$cast = $_POST['cast'];
$title = $_POST['title'];
$comment =$_POST['comment'];
$year = $_POST['year'];
$tag = $_POST['tags'];
$IDBM = $_POST['idbm'];
$cast = htmlspecialchars($cast);
$title = htmlspecialchars($title);
$comment = htmlspecialchars($comment);
// validate input
$valid = true;
if (empty($cast)) {
$castError = 'Please enter Cast';
$valid = false;
}
if (empty($title)) {
$titleError = 'Please enter Title';
$valid = false;
}
if (empty($comment)) {
$commentError = 'Please enter Comment';
$valid = false;
}
if ($valid) {
$id = $_REQUEST['id'];
$valid_formats = array("jpg", "png", "gif", "bmp");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
if($size<(1024*1024))
{
$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
mysqli_query($dbc,"UPDATE movie SET Title='$title',Year = '$year',Cast='$cast',Cover='$actual_image_name',Tag='$tag',Comment='$comment',IMDB ='$IDBM' WHERE MovieID=".$id);
header ("Location: index.php");
}
else
echo "failed";
}
else
echo "Image file size max 1 MB";
}
else
echo "Invalid file format..";
}
else
echo "Please select image..!";
exit;
}
}
}
First thing, when you get a blank page, check your error log. Or if you're lazy, add this at the begining of your file to get error messages.
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
?>
It's hard to say, but just looking at your code quickly, I see a problem with your mixup of $_GET and $_POST. From what I gather, since your SELECTworks, you send data in $_GET, and your UPDATE block is only executed if you have $_POST data.
Change your html <form method="get"> for <form method="post">
And change your select block to check if( !empty($_POST['id'])) {
Ok So i have an PHP page and i have a database.
In my database i have a table with a Field one of the fields is called accounttype it is enum('n', 'm', 's')
I am trying to display on my PHP page if the user is N it should say Normal User if the user is E Expert user or S Super user...
How do i go about doing this?
Top of PHP Page
<?php
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM members WHERE id='$id' LIMIT 1");
while($row = mysql_fetch_array($sql)){
$phone = $row["phone"];
$country = $row["country"];
$state = $row["state"];
$city = $row["city"];
$accounttype = $row["accounttype"];
$bio = $row["bio"];
}
?>
Where i am trying to display on the page This is the code. Right now it just puts a blank space.
<span class="admin">Edward</span>
<span class="date">March 19, 2048</span>
<span class="tag"><?php echo "$accounttype"; ?></span>
<span class="comment">166 comments</span>
picture
http://i.stack.imgur.com/KXu9A.png
first make a connection, than dont make a while, make a if like this
if($row = mysql_fetch_array($sql)){
$phone = $row["phone"];
$country = $row["country"];
$state = $row["state"];
$city = $row["city"];
$accounttype = $row["accounttype"];
$bio = $row["bio"];
}
and than
$speaking_type = null;
switch($accounttype) {
case 'n':
$speaking_type = 'Normal User';
break;
case 'm':
$speaking_type = 'Expert User';
break;
case 's':
$speaking_type = 'Super User';
break;
default:
$speagink_type = 'none';
//throw new Exception('unsupported account type '.$accounttype);
}
echo $speaking_type;
I think the problem is your scope. Your variables are defined within the while-loop, and so they are unknown further in the document. Try instantiating them on top (before the while-loop) like this:
$phone = null;
$country = null;
$state = null;
$city = null;
$accounttype = null;
$bio = null;
Than the variables will be known outside the while and the values will be remembered when you print them.
I thought u didn't connect to the database first .use following code to the connect with your credentials.That's why you are seeing a blank space
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
mysql_close($con);