Warning: Illegal string offset 'id' in - php

I'm making a form to update my MySQL database, but for some reason I get the following error in my browser:
C:\wamp\www\Helpdeskapplicatie\update_hardware.php on line 49
Could someone tell me what I'm doing wrong??
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/layout.css"/>
</head>
<body>
<div id="menu">
<div id="menu_wrapper">
<ul>
<li>Configuratiebeheer<img src="afb/pijltje.png" width="10"/></a>
<ul>
<li>Lijst hardware</li>
<li>Hardware toevoegen</li>
<li>Hardware verwijderen</li>
</ul>
</li>
</ul>
</div>
</div>
<?php
$connect=mysql_connect("localhost", "root","");
mysql_select_db("helpdesk_middenpolder", $connect);
$id=$_GET['id'];
$q="SELECT * FROM hardware WHERE hardwareID=$id";
$r=mysql_query($q);
echo "<table border='1'>";
echo "<th>merknaam</th><th>producttype</th><th>hardwaretype</th>";
while ($x=mysql_fetch_array($r)){
echo "<tr>";
echo "<td>";
echo "<input type='text' value='".$x['merknaam']."'>";
echo "</td>";
echo "<td>";
echo "<input type='text' value='".$x['producttype']."'>";
echo "</td>";
echo "<td>";
echo "<input type='text' value='".$x['hardwaretype']."'>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($connect);
?>
<?php
if(isset($_POST['updatehardware'])){
$query = "UPDATE hardware SET merknaam='".$x['merknaam']."', producttype='".$x['producttype']."', hardwaretype='".$x['hardwaretype']."' WHERE hardwareID='".$id['id']."'";
mysql_query($query);
}
?>
<form method="post">
<input type="submit" name="updatehardware" value="Hardware updaten">
</form>
</body>
</html>

Your update query need to change like this,
hardwareID='".$id."'
instead of
hardwareID='".$id['id']."'

You are using $id['id'] instead of $_GET['id']. However never trust passed with GET/POST data. Use instead:
$id = (int) $_GET['id'];

Related

How to show MySQL query results using PHP in box-style?

I'm setting up a new Website and i want to show the results of a searching MySQLi query in boxes.
My php code....
$query = mysqli_query($con,"SELECT * FROM `products`");
while($row = mysqli_fetch_array($query)) {
echo "<img src='/Images/".$row['product_image']."' height='200' width='200'>"
echo '<h6> Id:'.$row['product_id'].'</h6>';
echo '<h6> Name:'.$row['product_name'].'</h6>';
echo '<h6> Proce:'.$row['product_price'].'</h6>';
}
With that code the results of a searching query are presented like this
I'm trying using with many ways to achieve a presentation of the results like this way:
Any idea or advice? Thank you
Use css "float: left"
$query = mysqli_query($con,"SELECT * FROM `products`");
while($row = mysqli_fetch_array($query)) {
echo '<div style="float:left">';
echo "<img src='/Images/".$row['product_image']."' height='200' width='200'>";
echo '<h6> Id:'.$row['product_id'].'</h6>';
echo '<h6> Name:'.$row['product_name'].'</h6>';
echo '<h6> Proce:'.$row['product_price'].'</h6>';
echo '</div>';
}
you can use bootstrap to achieve this. For example, just add class="col lg-4" inside the div tag, and you will have 3 columns for each row.
Sample code:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<section class="content">
<div class="container">
<div class="row">
<?php
$get = mysqli_query($con, "SELECT * FROM `products` ORDER BY `product_name`");
while ($products = mysqli_fetch_assoc($get)) { ?>
<div class="col-lg-4">
<div class="thumbnail" align="center">
<img src="admin/images/products/<?php echo $products['photo']; ?>" style="width: 220px; height: 150px">
<div class="caption">
<h4>
<?php echo $products['product_name']; ?>
</h4>
<span>$<?php echo number_format($products['price']); ?></span>
<br />
<br />
Detail
<input type="button" name="wishlist" id="add_wishlist" value="+ " onclick="addWishlist('<?php echo $products[id]; ?>', '<?php echo $id_user; ?>')" class="btn btn-info">
</div>
</div>
</div>
<?php } ?>
</div>
</div>
</section>

Same page is loading when clicking pagination in DataTable PHP

When I click page number in DataTable, it is not moving to next page instead it loads the whole page again.
But if I click the DataTable sorting button and again I click the pagination page number, the DataTable is working fine. Can anyone help me to solve this. Thanks in advance.
<?php include 'header.php'; ?>
</style>
<!-- Breadcrumbs -->
<section class="bg-gray-7">
<div class="breadcrumbs-custom box-transform-wrap context-dark">
<div class="container">
<h3 class="breadcrumbs-custom-title">Sailing Schedules</h3>
<div class="breadcrumbs-custom-decor"></div>
</div>
<div class="box-transform" style="background-image: url(images/bg-typography.jpg);"></div>
</div>
<div class="container">
<ul class="breadcrumbs-custom-path">
<li>Home</li>
<li class="active">Sailing Schedule</li>
</ul>
</div>
</section>
<br>
<br>
<div class="container">
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
// Include config file
require 'config.php';
$sql = "SELECT * FROM long_sched";
if($result = mysqli_query($con, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='table-custom table-custom-primary' id ='long_schd_table'>";
echo "<thead>";
echo "<tr>";
echo "<th>Service Name</th>";
echo "<th>Description</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td><a href=http://localhost/permatest/". $row['long_filepath'] . " target='_self' download>".$row['long_service']."</a></td>";
echo "<td>" . $row['long_desc'] . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
// Free result set
mysqli_free_result($result);
} else {
echo "<p class='lead'><em>No records were found.</em></p>";
} else {
echo "ERROR: Could not able to execute ".$sql." " . mysqli_error($con);
}
// Close connection
mysqli_close($con);
?>
</div>
</section>
<br>
<br>
<?php include 'off_footer.php'; ?>
<script>
$('#long_schd_table').DataTable({});
</script>
The above code is giving this error but in my console I didn't get any error information.

Repeated Record in php and mysql

I am trieng to list item form the database, however, the result comes out in two. 2 result is displayed for each record. currently i only have one record in the database but when i display them on the screen it is repaeted. i do not know why this is happening, i have tried to palce a counter and see how many times the loop goes on and the result is always once. however the result is displayed two times.
the code as follows:
<section class="container">
<div class="row">
<?php
if($bidCollection->selectBidsByStatusAndAppId(BidAccount::OPEN_NEW, $id)){
foreach($bidCollection->getBids() as $bid){
$banker->find($bid->getCustomerId());
$counter=0;
?>
<div class='col-xs-12 bg-more-light-gray bidlist'>
<div class="col-xs-12 col-sm-1 col-md-1">
<?php
if(!$uploader->findProfilePicture($banker->data()->_customer_id)){
echo "<img src='image/holder.png ' width='50' height='50' class='img-responsive' />";
}else{
echo "<img src='upload/proPicture/".$uploader->data()->pictureUrl."' width='50' height='50' class='img-responsive'/>";
}
?>
</div>
<div class="col-xs-12 col-sm-8 col-md-8">
<?php
echo '<h2>Agent name :'.$validate->cleanInput($banker->data()->officer_name).'</h2>';
echo '<h3>Institute name : '.$validate->cleanInput($banker->data()->bank_name).' '.++$counter.'</h3>';
if($bid->getApplicationOwnerId() === $customer->data()->_customer_id){
echo "<p>CheckBook: ";
if($bid->getRequestCheckBook()){echo "Yes";}else{echo "No";}
echo "</p>";
echo"<p> Minimum Deposit: AED ".$bid->getMinDeposit()."</p>";
echo"<p> Direct Debit Card: ";
if($bid->getRequestCreditCard()){echo "Yes";}else{echo "No";}
echo "</p>";
echo"<p> Other Fees / Arrangement Fees: AED ".$bid->getFees(). "</p>";
echo"<p> Account Will be ready in: ".$bid->getSetupTime() ."</p>";
}
?>
</div>
<div class="col-xs-12 col-sm-2 col-md-2">
<?php
$datetime = new DateTime($bid->getDatePosted());
$date = $datetime->format('Y-m-d');
$time = $datetime->format('H:i:s');
echo '<h4> Date: '.$date.'<br/>Time: '.$time.'</h4>';
echo"<form method='POST' action='viewBanker.php'>";
echo "<input type='hidden' name='banker' value='".$bid->getCustomerId()."'>";
echo "<input type='submit' class ='btn btn-default' name='submit' value='View Profile'>";
echo "</form>";
echo'<br/>';
if($bid->getApplicationOwnerId() === $customer->data()->_customer_id){
echo"<form method='post' action='acceptAccountBid.php'>";
echo "<input type='hidden' value='".$bid->getCustomerId()."' name ='bankerId'/>";
echo "<input type='hidden' value='".$id."' name ='appid'/>";
echo "<input type='hidden' value='".$bid->getApplicationOwnerId()."' name='ownerId'>";
echo "<input class='btn btn-default' type='submit' value='Accept Offer' name='submit'>";
echo"</form>";
}
?>
</div>
</div>
<?php
}
}
?>
The Methods that returns the the values is as follows:
public function selectBidsByStatusAndAppId($status, $appid)
{
$sql = "SELECT * FROM accountBid WHERE application_id = :appId AND status = :st";
try {
$sth = $this->_db->getConnection()->prepare($sql);
$sth->bindValue(':appId', $appid);
$sth->bindValue(':st', $status);
$sth->execute();
} catch (Exception $e) {
$this->setAlert('danger', 'Information Presentation Error: ' . $e->getMessage());
}
foreach ( $sth->fetchAll(PDO::FETCH_ASSOC) as $data) {
$this->addBid($data);
}
return true;
}
public function addBid($data = null)
{
$bid = new BidAccount($data);
$this->bids[] = $bid;
}
public function getBids()
{
return $this->bids;
}
first of all, I want to thank my mates who tried to help me earlier. the problem was I had another function which I forgot to post it here bidExist()
public function bidExist( $appId , $id){
$sql = "SELECT * FROM accountBid WHERE application_id = :appId AND _customer_id = :id";
try {
$sth = $this->_db->getConnection()->prepare($sql);
$sth->bindValue(':appId', $appId);
$sth->bindValue(':id', $id);
$sth->execute();
} catch (Exception $e) {
$this->setAlert('danger', 'Information Presentation Error: ' . $e->getMessage());
}
foreach ( $sth->fetchAll(PDO::FETCH_ASSOC) as $data) {
$this->addBid($data);
}
return $sth->rowCount();
}
The function above looped through the collection to check and see if the object exists before this and if it is then you can not add anymore bid, however, i forgot to remove the addBid() function on this method and that cause the object to be added twice to the collection. This took me the whole night to figure out. but I thank everyone who tried to support and assist.

Saving Radio buttons info on php

I'm currently working on a voting project, i have a form that is populated from the database and i managed to use loops to generate radio buttons. However the radio button can only select one answer out of the possible eight, i need it to select an answer in each and every question. i know the radio button names are the same and the need to be different, however its a loop so i dont know how to change the names. any help?
Here is the code
<?php $title="POLLS | Voting - Home";
require_once( "includes/session.php");
include( "includes/connection.php");
include( "includes/header.php");
$query="SELECT user_id FROM users WHERE user_id={$_SESSION['user_id']}" ;
$result=mysql_query($query,$connection);
$row=mysql_fetch_row($result);
$_SESSION[ 'user_id']=$row[0]; ?>
<body>
<div id="outerDiv">
<div id="header">
<div id="mainMenu"> </div>
<!-- mainMenu -->
</div>
<!-- header -->
<div id="midBanner"> </div>
<div id="content">
<div id="mainContent">
<p>Welcome,
<?php echo strtoupper($_SESSION[ 'username'])?>!</p>
<form action="votingpage.php" method="post">
<?php $query="SELECT question_id,question FROM questions " ; $result=mysql_query($query, $connection); ?>
<form>
<table class="Question Table">
<th></th>
<?php echo "<tr>"; echo "<tr>"; echo "<tr>"; ***strong text***$num_rows="" ; $counter=$num_rows; while($row=mysql_fetch_assoc($result)){ echo "<br />"; $counter +=1 ; echo "<br />"; echo $counter; echo $row[ 'question']; echo "<br />"; $query1="SELECT qstn_ans_id, possible_answer FROM qanswers WHERE question_id= " . $row[ 'question_id'] ; $result1=mysql_query($query1, $connection); while($row1=mysql_fetch_assoc($result1)){ echo "<input type='radio' name='possible_answer'".$row1[ 'qstn_ans_id']. " value='".$row1[ 'qstn_ans_id']. "'/> ".$row1[ 'possible_answer']; echo "<br />"; } } ?> </table>
</form>
</div>
<!-- mainContent -->
<p class="clear" /> </div>
<!-- content -->
</div>
<!-- outerDiv -->
<?php include( "includes/footer.php"); ?>

PDO Messed Up My CSS / HTML Layout

One of my webpages that had perfect format under MySQL_* functions, suddenly got mixed up when I translated the old functions to PDO. The problem is that my Copyright bar is now floating above my table of MySQL data, while it should be at the bottom of the page. I'm not sure why it's happening. Any help is appreciated!
This is the portion of the file that I believe may be the problem:
<div id="content" style="float:center;background-image:url('http://tinyurl.com/bds4u2j');">
<ul class="menu">
<li>
<?php
if(!isset($_COOKIE['username'])){
echo "Welcome, guest!";
}else{
echo "Welcome, " . $_COOKIE['username'];
}
?>
</li>
<li><br></li>
<li><a class="item" href="index.html">Home</a></li>
<li><a class="item" href="classes.html">Classes</a></li>
<li><a class="item" href="contact.html">Contact</a></li>
<?php
if(!isset($_COOKIE['username'])){
echo '<li><a class="item" href="login.html">Login</a></li>';
}else{
echo '<li><a class="item" href="upload.html">Upload</a></li>';
echo '<li><a class="item" href="password_protect.php?logout=1">Logout</a></li>';
}
?>
</ul>
<center><h1>Mr. Stanford's first period</h1></center>
<br>
<center><h3>Your class files are available for download below</h3></center>
<br>
<br>
<center>
<?php
echo "<table border='1'>
<tr>
<th>Filename</th>
<th>Description</th>
<th>Download</th>";
IF(isset($_COOKIE["username"]))
{
echo "<th>Delete</th>","</tr>";
}
ELSE
{
echo "</tr>";
}
try{
$dbh = new PDO('mysql:host=$host;port=$port;dbname=$dbname','$username','$password');
foreach($dbh->query('SELECT * from period1') as $row) {
echo "<tr>";
echo "<td>" . $row['Filename'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td><a href = ". $row['File'] ." ><button>Download</button></a></td>";
IF(isset($_COOKIE["username"]))
{
echo "<td><a href = 'deletefile.php?$s_id=". $row['s_id'] ."'><button>Delete</button></a></td>","</tr>";
}
ELSE
{
echo "</tr>";
}
echo "</table>";
}
$dbh=null;
} catch (PDOException $e) {
print "<br>ERROR: " . $e->getMessage() . "<br/>";
die();
}
$dbh=null;
?>
</center>
<br>
<br>
<br>
</div>
<div id="footer" style="background-color:#99FF33;clear:both;text-align:center;">
Copyright © David Schilpp 2013, All Rights Reserved
</div>
Again, thank you to anybody who can help me debug this problem!
I guessed that your table was not closed. see comments on question.
When you are facing this kind of problem (broken html), you should first analyse the html generated.
You can right-click on page and select View page source.
You can use tools freely available too.
As always, StackOverflow is great place to find answer and new stuff

Categories