How to debug a MySQL error in PHP? - php

I have code that normally don't have any problem with but somehow the MySQL query part (marked with an arrow ------>) is stopping the code.
The MySQL command is fine and the mysql_connection is working, I can ping it.
I'm working all the time like that, I just can't find the error, and since the code is completely stopping I can't fetch any error message.
<?php
//including the mysql_connection
include("../mechanics/mysql_con.php");
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../mechanics/segments_container.css"/>
</head>
<body>
<div class = "container">
<div class = "titlebar">
Members
</div>
<div class = "inner_container">
<center>
<table style="color:#CCCCCD;width:100%;">
<tr>
<td style="min-width:50px;text-align:center;"><b>Avatar</b></td>
<td style="min-width:150px;text-align:center;"><b>Username</b></td>
<td style="min-width:50px;text-align:center;"><b>Joined</b></td>
<td style="min-width:50px;text-align:center;"><b>Posts</b></td>
<td style="min-width:50px;text-align:center;"><b>Reputation</b></td>
</tr>
<?php
$mysql_get_users = "SELECT avatar, name, registerdate, posts, reputation FROM members";
-----> $data = $mysqli->query($mysql_get_users);
-----> while($row = $data->fetch_array()){
?>
<tr>
<td><?php echo $row["avatar"]; ?></td>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["registerdate"]; ?></td>
<td><?php echo $row["posts"]; ?></td>
<td><?php echo $row["reputation"]; ?></td>
</tr>
<?php
}
?>
</table>
</center>
</div>
</div>
</body>
</html>

I think I found the answer.
Because the file memberlist.php itself is a include from the index.php, the directory wasn't ../mechanics/mysql_con.php instead it was mechanics/mysql_con.php
I guess because of the including the path started from index and not the it's folder segments (where memberlist.php is in)
Thanks for every second. :)

Related

Retrieve Server Data from a mysql db

I have question about how i shall do this.
First i have taken out a list from a mysql database which.
What I want to do is to be able to click on one of these items in the list so that I can get the underlying facts for this particular item.
Now i want to use this as a link to get one row from the database.
The database is done and the list i can see in a website but i dont get the last part to work
show_all_symbol.php
//This show the list
<?php
include 'connect.php';
$sql = "SELECT * FROM t1_symbolism ";
$q = $pdoconn->query($sql);
$q ->setfetchmode(PDO::FETCH_ASSOC);
?>
<table>
<head>
<tr><th>Symbol</th><th>Alternativtnamn</th> <th>Symboltyp</th></tr>
</thead>
<tbody>
<?php while ($row = $q->fetch()): ?>
<td><img src="<?php echo "/symbol/",$linksbild,($row['symb_type']),"/",($row['symb_pic']); ?>"/></td>
<tr>
<td> <a id="leftside_meny" href= <?php echo "/symbol/show_symbol.php";?> target= "texten"> <?php echo ($row['symb_name']); ?></a></td>
<td><?php echo ($row['symb_name_other']); ?></td>
<td><?php echo ($row['symb_type']); ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</body>
show_symbol.php
<body>
<?php
$funamn = trim($_POST['symbname']);
include 'connect.php';
$sqlanswer = $pdoconn->query(" SELECT * from t1_symbolism where symb_name '$funamn'");
$row = $sqlanswer->fetch();
?>
<table >
<thead>
<tr><th>Symbol</th><th>Symbolnamn</th><th>Alternativtnamn</th> <th>Symboltyp</th><th>Fakta</th></tr>
</thead>
<tr>
<td><img src="<?php echo "/symbol/",$linksbild,($row['symb_type']),"/",($row['symb_pic']); ?>" width="250" height="250"/></td>
<td><?php echo ($row['symb_name']); ?></td>
<td><?php echo ($row['symb_name_other']); ?></td>
<td><?php echo ($row['symb_type']); ?></td>
<td><?php echo ($row['symb_history']); ?></td>
</tr>
</table>
</body>
What i want to do is to use this part as Use this part as a clickable link so I can view a record in the table.
<td> <a id="leftside_meny" href= <?php echo "/symbol/show_symbol.php";?> target= "texten"> <?php echo ($row['symb_name']); ?></a></td>
Thank in the advance!
it looks like you are looking for a way to pass this value $row['symb_name'] to this value $_POST['symbname']. Here's what i think you should try :
in your show_all_symbol.php
<td>
<a id="leftside_meny" href="<?php echo "/symbol/show_symbol.php?symb_name=" . $row['symb_name'];?>" target="texten">
<?php echo ($row['symb_name']); ?>
</a>
</td>
in your show_symbol.php
$funamn = trim($_GET['symb_name']);
If you want to use $_POST then you will have to either figure a way to make a form out of each clickable element and configure it to make a POST or do it using javascript or jquery.

How use session variable in sql using php

I have a problem with displaying files added by the logged user.
I do not know how to pass the variable correctly to the sql query.
Can anyone help me with this?
Currently, the code looks like this:
<?php
include_once 'dbconnect.php';
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>File Uploading With PHP and MySql</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
<label>File Uploading With PHP and MySql</label>
</div>
<div id="body">
<table width="80%" border="1">
<tr>
<th colspan="4">your uploads...<label>upload new files...</label></th>
</tr>
<tr>
<td>File Name</td>
<td>File Type</td>
<td>File Size(KB)</td>
<td>View</td>
</tr>
<?php
$sql="SELECT * FROM files";
$result_set=mysql_query($sql);
while($row=mysql_fetch_array($result_set))
{
?>
<tr>
<td><?php echo $row['file'] ?></td>
<td><?php echo $row['type'] ?></td>
<td><?php echo $row['size'] ?></td>
<td>view file</td>
</tr>
<?php
}
?>
</table>
</div>
</body>
</html>
I am trying to change this record :
$sql="SELECT * FROM files";
to
$sql="SELECT file, type, size FROM files WHERE userId ='$_SESSION[userId]'";
but I still do not get the correct result. Can anyone help?
It looks like the issue with that line is in how you are including the $_SESSION variable. You should have quotes around userId like $_SESSION['userId'] or {$_SESSION['userId']}.
More importantly you should avoid entering variables directly into MySQL queries. I would recommend using MySQLi or PDO instead of MySQL, and look into prepared statements (here or here, for example).

mysqli_fetch_assoc() not working, unable to enter loop

I have been trying to display the data from a table onto a page. The table is called events and the database is school.
As far as I know, I'm doing everything right, I've stored the query in a string, run it using mysqli_query() and later put the echo statements in a while loop while placing $row=mysqli_fetch_assoc($result) inside the parenthesis. Yet when I run it not even a single entry is displayed on the page.
Here's my entire code:
<?php
require("includes/common.php");
$query = "SELECT name,place,date FROM school.events";
$result = mysqli_query($con, $query)or die(mysqli_error($con));
?>
<!DOCTYPE html>
<!--
Calendar Page for Trinity High School
-->
<html>
<head>
<title>Events Calendar</title>
<?php
require 'includes/external.php';
?>
</head>
<body>
<div class="content">
<?php
include 'includes/header.php';
?>
<div class="container">
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Event</th>
<th>Place</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['place']; ?></td>
<td><?php echo $row['date']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</body>
</html>
PS: Using some trial and error I found out that the program simply doesn't enter the while loop. Perhaps there's something wrong with the condition?

echo the any particular client to particular project

I have created a simple Business Management setup for office work. First I fetch all information from database. All is going well except the quantity input.
i creat a simple client-Payment System in php. in this first Add any client it's doing simple and all client show at a simple page when click a client name then this page go on the project page when add new project name then all project are show on this page. I want the perticualr client name to pericular project show
<?php session_start(); include("sidebar.php"); include "config.php"; $sel="select * from new_project"; $rs=mysql_query($sel); ?>
<div class="h_right">
<h1> +ADD New Project </h1>
</div>
<div class="h_left">
<table border="2px" cellpadding="10px">
<tr>
<td>Id</td>
<td>Project Name</td>
</tr>
<?php while($rec=mysql_fetch_assoc($rs)) { ?>
<tr>
<td><?php echo $rec['id'] ?></td>
<td><a href="projectdetail.php?><?php echo $rec['project_name'] ?></a></td>
<?php } ?>
</table>
</div>
Use a foreach loop:
<?php session_start(); include("sidebar.php"); include "config.php"; $sel="select * from new_project"; $rs=mysql_query($sel); ?>
<div class="h_right">
<h1> +ADD New Project </h1>
</div>
<div class="h_left">
<table border="2px" cellpadding="10px">
<tr>
<td>Id</td>
<td>Project Name</td>
</tr>
<?php foreach($rec=mysql_fetch_assoc($rs) as $project) { ?>
<tr>
<td><?php echo $rec['id'] ?></td>
<td><?php echo $project['project_name'] ?></td>
<?php } ?>
</table>
</div>
new_project.php doesn't need a project id
projectdetail.php will display project based on the project id passed

Add items to a cart from an array

I'm creating a php driven shopping cart as part of a task.
All products are currently stored in an array (I'm aware they would realistically be stored in a database, however for this task it isn't required)
I would like to know if its possible to add items to a cart from clicking a link, upon clicking proceed, it would take the user to a summary screen, displaying the cost of each item (value displayed from the array) and total cost of all items.
I am also looking to include a promotion code input box, is it possible to calculate and display a discount on button click, E.G "Update"
So far, I only have the products displayed in a table which is ok, the cart section is confusing me at the moment as well as the discount section.
<?php
$Item1 = array('SKU'=>test1, 'name'=>ProductTest1, 'Price'=>10.00);
$Item2 = array('SKU'=>test2, 'name'=>ProductTest2, 'Price'=>11.00);
$Item3 = array('SKU'=>test3, 'name'=>ProductTest3, 'Price'=>12.00);
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="Style.css">
</head>
<body>
<div id="container">
<div id="main">
<table>
<tr>
<th>SKU</th>
<th>Name</th>
<th>Price</th>
<th>Action</th>
</tr>
<tr>
<td><?php echo $Item1[SKU]; ?></td>
<td><?php echo $Item1[name]; ?></td>
<td><?php echo '£'. number_format($Item1[Price],2); ?></td>
<td><a href=#>Add To Cart</a></td>
</tr>
<tr>
<td><?php echo $Item2[SKU]; ?></td>
<td><?php echo $Item2[name]; ?></td>
<td><?php echo '£'. number_format($Item2[Price],2); ?></td>
<td><a href=#>Add To Cart</a></td>
</tr>
<tr>
<td><?php echo $Item3[SKU]; ?></td>
<td><?php echo $Item3[name]; ?></td>
<td><?php echo '£'. number_format($Item3[Price],2); ?></td>
<td><a href=#>Add To Cart</a></td>
</tr>
</table>
<input id="Proceed" type="Submit" value="Proceed">
</div>
</div>
</body>
</html>
Your first port of call will be to look into sessions (http://www.w3schools.com/php/php_sessions.asp)
Your Add to Cart link will go through to a different PHP page, so it might be addtocart.php
Each Add to Cart link will pass through the SKU/Product ID, so the link for each will have a unique GET parameter - I.E. addtocart.php?sku=test1 - That way you know which Product you're adding to cart.
I'd read through a couple of tutorials from Google, like http://jameshamilton.eu/content/simple-php-shopping-cart-tutorial?PHPSESSID=99d373741727e3010a32319f1ebed001
If you're still having trouble, breakdown your question into specific points and I'm sure someone will help out.
See Simple PHP Shopping Cart without SQL for creating a shopping cart without a database.
At first this is not good way to use the array like this. Try this:
<?php
$products = array(
array('SKU'=>test1, 'name'=>ProductTest1, 'Price'=>10.00),
array('SKU'=>test2, 'name'=>ProductTest2, 'Price'=>11.00),
array('SKU'=>test3, 'name'=>ProductTest3, 'Price'=>12.00),
);
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="Style.css">
</head>
<body>
<div id="container">
<div id="main">
<table>
<tr>
<th>SKU</th>
<th>Name</th>
<th>Price</th>
<th>Action</th>
</tr>
<?php foreach ($products as $key => $product): ?>
<tr>
<td><?php echo $product['SKU']; ?></td>
<td><?php echo $product['name']; ?></td>
<td><?php echo '£'. number_format($product['Price'],2); ?></td>
<td>Add To Cart</td>
</tr>
<?php endif; ?>
</table>
<input id="Proceed" type="Submit" value="Proceed">
</div>
</div>
</body>
</html>
Now how to solve the cart. You have to use something persistent. The session will be easiest:
<?php
session_start();
$products = array(
array('SKU'=>test1, 'name'=>ProductTest1, 'Price'=>10.00),
array('SKU'=>test2, 'name'=>ProductTest2, 'Price'=>11.00),
array('SKU'=>test3, 'name'=>ProductTest3, 'Price'=>12.00),
);
if (isset($_GET['action'] && $_GET['action'] === 'addToCart') {
if (!isset($_SESSION['cart']) $_SESSION['cart'] = array();
$_SESSION['cart'][] = $_GET['product'];
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="Style.css">
</head>
<body>
<div id="container">
<div id="main">
<?php if (isset($_SESSION['cart']) && !empty($_SESSION['cart'])): ?>
<ul id="cart">
<?php foreach($_SESSION['cart'] as $product): ?>
<li><?= $products[$product]['name'] ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<table>
<tr>
<th>SKU</th>
<th>Name</th>
<th>Price</th>
<th>Action</th>
</tr>
<?php foreach ($products as $key => $product): ?>
<tr>
<td><?php echo $product['SKU']; ?></td>
<td><?php echo $product['name']; ?></td>
<td><?php echo '£'. number_format($product['Price'],2); ?></td>
<td>Add To Cart</td>
</tr>
<?php endif; ?>
</table>
<input id="Proceed" type="Submit" value="Proceed">
</div>
</div>
</body>
</html>
It should work. Just adapting your code.

Categories