PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
slade powers
Forum Newbie
Posts: 8 Joined: Sat Nov 20, 2010 2:46 pm
Post
by slade powers » Fri Jan 08, 2016 12:58 pm
hello, all. i haven't been here in a while but i'm seeking a little help. i'm having problems with the following code:
Code: Select all
try {
$dbh = new PDO("mysql:host=$hostname;dbname=new_order", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/*** echo a message saying we have connected ***/
echo 'Connected to database<br />';
}catch(PDOException $e) {
echo $e->getMessage();
}
?>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
$errors = array();
if(empty($errors))
// prepare sql and bind parameters
$dbh = $conn->prepare("INSERT INTO new_order (item, temple, quantity, price) VALUES(?,?,?,?)"); // it is at this line i am getting an error
$dbh->bind_param("ssss", $item, $temple, $quantity, $price);
$dbh->execute();
/*** close the database connection ***/
$dbh = null;
i'm missing something and i just don't see it yet. a little guidance, please. thank you so much.
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Fri Jan 08, 2016 2:17 pm
When you say "i just don't see it yet", do you mean there is no output? or that the INSERT is not working and no record is added to the data table?
(#10850)
slade powers
Forum Newbie
Posts: 8 Joined: Sat Nov 20, 2010 2:46 pm
Post
by slade powers » Fri Jan 08, 2016 2:49 pm
i meant that i just don't see what i keep doing wrong.
when i attempt to process this code:
Code: Select all
try {
$dbh = new PDO("mysql:host=$hostname;dbname=new_order", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/*** echo a message saying we have connected ***/
echo 'Connected to database<br />';
}catch(PDOException $e) {
echo $e->getMessage();
}
?>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
$errors = array();
if(empty($errors))
// prepare sql and bind parameters
$dbh = $conn->prepare("INSERT INTO new_order (item, temple, quantity, price) VALUES(?,?,?,?)");
$dbh->bind_param("ssss", $item, $temple, $quantity, $price);
$dbh->execute();
/*** close the database connection ***/
$dbh = null;
i get the error message attached.
any direction would be appreciated greatly.
slade powers
Forum Newbie
Posts: 8 Joined: Sat Nov 20, 2010 2:46 pm
Post
by slade powers » Fri Jan 08, 2016 2:54 pm
Connected to database
Notice: Undefined variable: conn in /home/ewff/public_html/prac0.php on line 56
Fatal error: Call to a member function prepare() on a non-object in /home/ewff/public_html/prac0.php on line 56
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Fri Jan 08, 2016 4:20 pm
Careful copying code from different sources. The problem is how you are using $dbh (your PDO connection object) and $conn (unknown object). Change the code to:
Code: Select all
$stmt = $dbh->prepare("INSERT INTO new_order (item, temple, quantity, price) VALUES(?,?,?,?)");
$stmt->bind_param("ssss", $item, $temple, $quantity, $price);
$stmt->execute();
If the name $dbh is not clear for you, then change it to $conn or $pdo or something.
(#10850)
slade powers
Forum Newbie
Posts: 8 Joined: Sat Nov 20, 2010 2:46 pm
Post
by slade powers » Fri Jan 08, 2016 6:46 pm
hello....by putting the code in the script i get a new error message:
Connected to database
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'new_order.new_order' doesn't exist' in /home/ewff/public_html/prac0.php:56 Stack trace: #0 /home/ewff/public_html/prac0.php(56): PDO->prepare('INSERT INTO new...') #1 {main} thrown in /home/ewff/public_html/prac0.php on line 56
this is the code:
Code: Select all
if ($_SERVER["REQUEST_METHOD"] == "POST")
$errors = array();
if(empty($errors))
// prepare sql and bind parameters
$stmt = $dbh->prepare("INSERT INTO new_order (item, temple, quantity, price) VALUES(?,?,?,?)");
$stmt->bind_param("ssss", $item, $temple, $quantity, $price);
$stmt->execute();
/*** close the database connection ***/
$dbh = null;
i'm reading up on what SQLSTATE[42S02] means
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Sat Jan 09, 2016 11:49 am
Don't get distracted with the numbers, read through the whole error message. The "readable" part of the message is "Table 'new_order.new_order' doesn't exist' ". You are connecting to a database new_order and inserting into a table called new_order. Could one of those names be incorrect (probably the table name, since the connection worked)?
(#10850)
slade powers
Forum Newbie
Posts: 8 Joined: Sat Nov 20, 2010 2:46 pm
Post
by slade powers » Sat Jan 09, 2016 12:26 pm
hello, all. kinda stuck here. i'm getting a bit turned around with regard to the database insert string. i'm getting the same error message as before.
Code: Select all
$dbname = 'new_order';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/*** echo a message saying we have connected ***/
echo 'Connected to database<br />';
}catch(PDOException $e) {
($ex->getMessage());
}
?>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
$errors = array();
if(empty($errors))
{
{
// prepare sql and bind parameters
$stmt = $dbh->prepare("INSERT INTO new_order (item, temple, quantity, price) VALUES(?,?,?,?)"); //this is line 59
$stmt->bind_param("ssss", $item, $temple, $quantity, $price);
$stmt->execute();
$host = $_SERVER['HTTP_HOST'];
$uri = $_SERVER['REQUEST_URI'];
header("Location: http://$host$uri");
}
}
/*** close the database connection ***/
$dbh = null;
?>
i know i'm close. but i'm still getting this error message: Connected to database
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'new_order.new_order' doesn't exist' in /home/ewff/public_html/prac01.php:59 Stack trace: #0 /home/ewff/public_html/prac01.php(59): PDO->prepare('INSERT INTO new...') #1 {main} thrown in /home/ewff/public_html/prac01.php on line 59
it appears that because line 59 is off the values can't be passed to the db. i'm still working on it. i'm sure the error is on my part. that's the only thing holding me back. thanks yall. i'll get it.
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Sun Jan 10, 2016 9:36 pm
Again, it says table new_order does not exist. What is the name of the table you are inserting item, temple, quantity, price into?
(#10850)
sladepowers
Forum Newbie
Posts: 6 Joined: Tue Dec 01, 2015 6:34 pm
Post
by sladepowers » Mon Jan 11, 2016 10:06 am
thanks for responding. i fixed that error but now i have another one that i have been working on for 2 days. i know i'm just missing something small but i get a error message.
Connected to database
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'Item' cannot be null' in /home/ewff/public_html/prac0.php:66 Stack trace: #0 /home/ewff/public_html/prac0.php(66): PDOStatement->execute() #1 {main} thrown in /home/ewff/public_html/prac0.php on line 66
here is the relevant code:
Code: Select all
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$errors = [];
}
if(empty($errors)) {
// prepare sql and bind parameters
$query = "INSERT INTO details (item, temple, quantity, price)
VALUES (:item, :temple, :quantity, :price)";
}
$stmt = $dbh->prepare($query);
$stmt->bindParam(':item', $_POST['item']);
$stmt->bindParam(':temple', $_POST['temple']);
$stmt->bindParam(':quantity', $_POST['quantity']);
$stmt->bindParam(':price', $_POST['price']);
$stmt->execute(); /***THIS IS LINE 66****/
/*** close the database connection ***/
$dbh = null;
?>
any assistance would be so much appreciated. thank you. once i can pass this data i'm on my way
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Mon Jan 11, 2016 1:04 pm
The error message is "Column 'Item' cannot be null'" so my guess is that $_POST['item'] is undefined because it is not passed.
(#10850)
sladepowers
Forum Newbie
Posts: 6 Joined: Tue Dec 01, 2015 6:34 pm
Post
by sladepowers » Mon Jan 11, 2016 1:57 pm
forgive my not understanding but does that mean the other three $_POST functions are passing data? netbeans doesn't show any error. i dont understand what you mean by the $_POST['item'] is undefined.
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Mon Jan 11, 2016 2:12 pm
We haven't seen your form, but it would appear that it is missing an input named 'item'.
sladepowers
Forum Newbie
Posts: 6 Joined: Tue Dec 01, 2015 6:34 pm
Post
by sladepowers » Mon Jan 11, 2016 3:08 pm
thanks so much for your time. i really appreciate it. here is the form:
Code: Select all
<form id="shoppingCart" method="POST" class="form-inline" name="basket_form" action="MyConnection">
<div class="content">
<div class="navbar">
<div class="navbar-inner">
<ul class="nav custom_nav">
<li class="pull-left">
<div>
<a href="http://xxxxwasxxxxco" class="btn btn-info">
<i class="icon-hand-left icon-white"></i>
Continue Shopping
</a>
</div>
</li>
<li class="pull-right">
<a href="http://solutionm3.com/checkout.html">
<span class="btn btn-success continue_to_checkout" type="submit" value="Checkout" name="cont_to_checkout1">
Continue To Checkout
</span>
</a>
</li>
<li class="pull-right"> </li>
<li class="pull-right">
<div>
<button class="btn btn-danger remove_all" type="submit" name="remove_all1" value="remove_all" rel="tooltip" data-placement="bottom" data-original-title="Be careful! All products will be entirely removed from the basket!">
<i class="icon-remove icon-white"></i>
Remove All
</button>
</div>
</li>
</ul>
</div>
</div>
<div class="basket_list clearfix">
<div class="tables">
<table class="table table_header">
<thead>
<tr>
<th colspan="2" class="description">
Item Description
</th>
<th class="size">
TEMPLE
</th>
<th class="quantity">
Quantity
</th>
<th class="price">
Price
</th>
<th class="subtotal">
Subtotal
</th>
<th>
</th>
</tr>
</thead>
</table>
<table class="table">
<tbody>
<tr class="warning">
<td class="thumb" rowspan="2"><a href="javascript: void(0);" title="Link to product page"><img src="img/331.jpg" alt="" width="123" height="91" title="Product Image"></a></td>
<td class="description" rowspan="2">
<a class="item-name" href="javascript: void(0);" title="Link to product page">The Carat</a>
<div>
<div class="item-availability"><em>These Diamond Shape Spectacles Accentuate Anyone's Style and Grace</em>.</div>
<div class="item-sku">MDL331</div>
</div>
</td>
<td class="size">
<select class="span2" id="prod2_size1" name="prod2_size1">
<option value="0">Size</option>
<option value="Buffalo Horn" selected>Buffalo Horn</option>
<option value="Gold">Gold</option>
<option value="Rose Gold">Rose Gold</option>
<option value="White Gold">White Gold</option>
<option value="Titanium">Titanium</option>
<option value="Platinum">Platinum</option>
<option value="Whale Bone">Whale Bone</option>
</select>
</td>
<td class="color">
<input type="hidden" id="prod2_color1" name="prod2_color1" value="no">
</td>
<td class="quantity">Qty: <input id="prod2_qty1" name="prod2_qty1" type="text" class="number" value="1" class="inline input-mini">
</td>
<td class="price">
<span class="currency">$</span><span class="amount">3000.00</span>
</td>
<td class="subtotal">
<span class="currency">$</span><span class="amount">3000.00</span>
</td>
<td class="remove">
<button class="btn btn-danger btn-mini" type="submit" name="prod2_remove1" value="remove_item" rel="tooltip" data-placement="left" data-original-title="Be careful! This item will be entirely removed from the basket!">
<i class="icon-remove icon-white"></i>
</button>
</td>
</tr>
<tr class="warning additional_empty">
<td class="empty_td" colspan="8">
</td>
</tr>
</tbody>
</table>
<table class="table">
<tbody>
<tr class="warning">
<td class="thumb" rowspan="2">
<a href="javascript: void(0);" title="Link to product page"><img src="img/332.JPG" width="123" height="91"></a></td>
<td rowspan="2" class="description">
<a class="item-name" href="javascript: void(0);" title="Link to product page">The Minimum</a>
<div>
<div class="item-availability">
<p><em>Timeless Design, </em><em>A true work of art</em>, <em>High Quality Lenses, </em><em>A Comfortable Fit</em></p>
</div>
<div class="item-sku">MDL332</div>
</div>
</td>
<td class="size">
<select class="span2" id="prod2_size1" name="prod2_size1">
<option value="0">Size</option>
<option value="Buffalo Horn" selected>Buffalo Horn</option>
<option value="Gold">Gold</option>
<option value="Rose Gold">Rose Gold</option>
<option value="White Gold">White Gold</option>
<option value="Titanium">Titanium</option>
<option value="Platinum">Platinum</option>
<option value="Whale Bone">Whale Bone</option>
</select>
</td>
<td class="color"><input type="hidden" id="prod2_color1" name="prod2_color1" value="no">
</td>
<td class="quantity">Qty: <input id="prod2_qty1" name="prod2_qty1" type="text" class="number" value="1" class="inline input-mini">
</td>
<td class="price">
<span class="currency">$</span><span class="amount">4000.00</span>
</td>
<td class="subtotal">
<span class="currency">$</span><span class="amount">4000.00</span>
</td>
<td class="remove">
<button class="btn btn-danger btn-mini" type="submit" name="prod2_remove1" value="remove_item" rel="tooltip" data-placement="left" data-original-title="Be careful! This item will be entirely removed from the basket!">
<i class="icon-remove icon-white"></i>
</button>
</td>
</tr>
<tr class="warning additional_empty">
<td class="empty_td" colspan="8">
</td>
</tr>
</tbody>
</table>
<table class="table">
<tbody>
<tr class="warning">
<td class="thumb" rowspan="2">
<a href="javascript: void(0);" title="Link to product page">
<img src="img/333.jpg" width="123" height="91" title="Product Image">
</a>
</td>
<td class="description" rowspan="2">
<a class="item-name" href="javascript: void(0);" title="Link to product page">The Maximum</a>
<div>
<div class="item-availability"><em>Rimless frames</em>, <em> Sophistication & Elegance</em> by Design</div>
<div class="item-sku">MDL333</div>
</div>
</td>
<td class="size">
<select class="span2" id="prod2_size1" name="prod2_size1">
<option value="0">Size</option>
<option value="Buffalo Horn" selected>Buffalo Horn</option>
<option value="Gold">Gold</option>
<option value="Rose Gold">Rose Gold</option>
<option value="White Gold">White Gold</option>
<option value="Titanium">Titanium</option>
<option value="Platinum">Platinum</option>
<option value="Whale Bone">Whale Bone</option>
</select>
</td>
<td class="color"><input type="hidden" id="prod2_color1" name="prod2_color1" value="no">
</td>
<td class="quantity"><label class="inline">Qty:</label><input id="prod2_qty1" name="prod2_qty1" type="text" class="number" value="1" class="inline input-mini">
</td>
<td class="price">
<span class="currency">$</span><span class="amount">5000.00</span>
</td>
<td class="subtotal">
<span class="currency">$</span><span class="amount">5000.00</span>
</td>
<td class="remove">
<button class="btn btn-danger btn-mini" type="submit" name="prod2_remove1" value="remove_item" rel="tooltip" data-placement="left" data-original-title="Be careful! This item will be entirely removed from the basket!">
<i class="icon-remove icon-white"></i>
</button>
</td>
</tr>
<tr class="warning additional_empty">
<td class="empty_td" colspan="8">
</td>
</tr>
</tbody>
</table>
<table class="table">
<tbody>
<tr class="warning">
<td class="thumb" rowspan="2"><a href="javascript: void(0);" title="Link to product page"></a><img src="img/POV_clip_image001.jpg" width="142" height="106"></td>
<td class="description" rowspan="2"><a class="item-name" href="javascript: void(0);" title="Link to product page">The Drop</a>
<div>
<div class="item-availability"><em>These Teardrop Cut Lenses with Lightweight Rimless frames are a work of art</em></div>
<div class="item-sku">MDL334</div>
</div></td>
<td class="size"><select class="span2" id="prod2_size3" name="prod2_size3">
<option value="0">Size</option>
<option value="Buffalo Horn" selected>Buffalo Horn</option>
<option value="Gold">Gold</option>
<option value="Rose Gold">Rose Gold</option>
<option value="White Gold">White Gold</option>
<option value="Titanium">Titanium</option>
<option value="Platinum">Platinum</option>
<option value="Whale Bone">Whale Bone</option>
</select></td>
<td class="color"><input type="hidden" id="prod2_color3" name="prod2_color3" value="no"></td>
<td class="quantity"><label class="inline">Qty:</label>
<input id="prod2_qty1" name="prod2_qty1" type="text" class="number" value="1" class="inline input-mini"></td>
<td class="price"><span class="currency">$</span><span class="amount">6000.00</span></td>
<td class="subtotal"><span class="currency">$</span><span class="amount">6000.00</span></td>
<td class="remove"><button class="btn btn-danger btn-mini" type="submit" name="prod2_remove1" value="remove_item" rel="tooltip" data-placement="left" data-original-title="Be careful! This item will be entirely removed from the basket!"> <i class="icon-remove icon-white"></i> </button></td>
</tr>
<tr class="warning additional_empty">
<td class="empty_td" colspan="8"></td>
</tr>
</tbody>
</table>
</div>
<div class="empty_basket">
Your Shopping Cart is empty!
</div>
<div class="order_blocks clearfix">
<div class="row">
<div class="span6 shipping_block clearfix">
<div class="delivery_block pull-left">
<h4>Delivery Method</h4>
<label class="radio">
<input type="radio" name="deliveryOption" id="deliveryOption1" value="0" checked>
STANDARD - 4-5 working days: <span class="currency">$</span>0.00
</label>
<label class="radio">
<input type="radio" name="deliveryOption" id="deliveryOption2" value="9.95">
EXPRESS DELIVERY - 48h: <span class="currency">$</span>9.95
</label>
</div>
<div class="promotional_block pull-right">
<h4>Promotional Code</h4>
<div class="input-append">
<input class="span2" id="appendedInputButton" name="promotional" value="" size="16" type="text" >
<button class="btn" type="submit" formaction="demo_admin.html" id="promotionAdd" rel="tooltip" data-placement="bottom" data-original-title="If you have a promotional code - you could add it here.">Add</button>
</div>
<h6>(Use any text here and you'll receive 10% of discount!)</h6>
</div>
</div>
<div class="span6 total_block">
<h4>Summary Block</h4>
<table id="basketTotalsList">
<tbody>
<tr>
<td class="total_position">Goods Subtotal</td>
<td class="basket_subtotal">
<span class="currency">$</span><span class="amount">325.67</span>
</td>
</tr>
<tr>
<td class="total_position">Taxes (<span class="taxes_percent">0</span>%)</td>
<td class="basket_taxes">
<span class="currency">$</span><span class="amount">325.67</span>
</td>
</tr>
<tr>
<td class="total_position">Delivery cost</td>
<td class="basket_delivery">
<span class="currency">$</span><span class="amount">325.67</span>
</td>
</tr>
<tr>
<td class="total_position">Discount (<span class="discount_percent">0</span>%)</td>
<td class="basket_discount">
-<span class="currency">$</span><span class="amount">325.67</span>
</td>
</tr>
<tr class="total">
<td class="total_position">
<h3>Order Total</h3>
</td>
<td class="basket_total">
<h3>
<span class="currency">$</span><span class="amount">325.67</span>
</h3>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="navbar">
<div class="navbar-inner">
<ul class="nav custom_nav">
<li class="pull-left">
<div>
<a href="http://xxxxwasxxxxco" class="btn btn-info">
<i class="icon-hand-left icon-white"></i>
Continue Shopping
</a>
</div>
</li>
<li class="pull-right">
<a href="http://solutionm3.com/checkout.html">
<span class="btn btn-success continue_to_checkout" type="submit" value="Checkout" name="cont_to_checkout1">
Continue To Checkout
</span>
</a>
</li>
<li class="pull-right"> </li>
<li class="pull-right">
<div>
<button class="btn btn-danger remove_all" type="submit" name="remove_all2" value="remove_all" rel="tooltip" data-placement="top" data-original-title="Be careful! All products will be entirely removed from the basket!">
<i class="icon-remove icon-white"></i>
Remove All
</button>
</div>
</li>
</ul>
</div>
</div>
</div>
</form>
i changed "item" to "description". i also changed it in mysql. yet i get the same error message: Integrity constraint violation: 1048 Column 'description' cannot be null'
thanks
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Mon Jan 11, 2016 3:32 pm
There is no form field named "item" in your form. I see form fields
prod2_color1 ,
prod2_qty1 ,
prod2_color3 ,
prod2_size1 and
prod2_size3 . You code above is receiving
item ,
temple ,
quantity and
price :
Code: Select all
$stmt->bindParam(':item', $_POST['item']);
$stmt->bindParam(':temple', $_POST['temple']);
$stmt->bindParam(':quantity', $_POST['quantity']);
$stmt->bindParam(':price', $_POST['price']);
The HTML form sends data by name. You PHP script receives data by name. The names need to match.
(#10850)