My problem is not connecting my select box to my database but getting the info to show.... It connects and i can see the boxes for the amount of items i have in my database table but there is no text in any of them. My database table is .CSV im not sure if that could cause a problem?
Here is my code: In my code i will just put in dummy_.... (What ever item) instead of the real thing.
<?php
require_once('auth.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<link href="styles/stylesheet.css" rel="stylesheet" type="text/css">
<head>
</head>
<body>
<form id="dropdown">
<?php
mysql_connect("localhost", "repaiami_member", "zmozmozm2083") or die("Connection Failed");
mysql_select_db("repaiami_member")or die("Connection Failed");
?>
<?php
$query = "SELECT * FROM lighting";
$result = mysql_query($query);
?>
<table width="450px;">
<tr>
<td>
<select id="dropdown_description" name="select1" class="ui-select selBox">
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<option value="<?php echo $line['field'];?>"> <?php echo $line['field'];?></option>
<?php } ?>
</select>
</td>
<td>
<input type="text" name="first_name" maxlength="50" size="30">
</td>
</tr>
</table>
</form>
</body>
</html>
Can anyone help?
Related
I create search.php and create database war and table product.
My code doesn't work:
<!DOCTYPE html>
<html>
<head>
<title>Search</title>
</head>
<body>
<form method="get" action="">
<table>
<tr>
<td>متن برای جستجو</td>
<td><input type="text" name="text"></td>
</tr>
<tr>
<td><input type="submit" value="Search"></td>
<td>
<?php
if(isset($_GET['text']) && !empty($_GET['text'])){
$body=$_GET['text'];
$con=mysql_connect("localhost","root","");
if(!$con){die("mysql Error");}
if (!mysql_select_db("war",$con)){die("mysql select error");}
$res=mysql_query("SELECT * FROM product WHERE LIKE pname='%$body%'");
$count=mysql_num_rows($res);
if ($count <= 0){
die("Your product Not found");
}else{
while ($row=mysql_fetch_array($res)){
echo $row['pname'];
}
}
}
?>
</td>
</tr>
</table>
</form>
</body>
</html>
Please help and debug.
Your MySQL SELECT statement is wrong. It should be:
$res=mysql_query("SELECT * FROM product WHERE pname LIKE '%$body%'"); // note the absence of =
If this is, of course, what you mean by "it doesn't work".
PS : do not use mysql_* functions. They are deprecated.
I have tried to insert html table multiple row data to database but not success without error.
Please refer below for my code and kindly advice what do I missed.
Retrieve data for dropdown selection
<?php
//including the database connection file
include_once("config_db.php");
$gettminfo=mysql_query("SELECT * FROM tb_tm ORDER BY tm_abb");
?>
Html Code for the table
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/spa_invoice.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<!--Add & delete rows from table dynamically-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(function(){
$('#addRow').click(function(){
var html = $('#row_template').html();
$('#dataTable').append(html);
$(".tablerow").each(function(index) {
$(this).html(index + 1);
});
});
$('#deleteRow').click(function(){
$('#dataTable .mychkbox:checked').parents('tr').remove();
calc_ttl();
});
$('#dataTable').on('change','.select-desc',function(){
var cur_val = $(this).val();
$(this).parents('tr').find('input[name="order_unit_prc[]"]').val(cur_val);
});
$('#dataTable').on('keyup','input[name="order_qty[]"]', function(){
var qty = +$(this).val();
var unit = +$(this).parents('tr').find('input[name="order_unit_prc[]"]').val();
$(this).parents('tr').find('input[name="order_amt[]"]').val(qty*unit);
calc_ttl();
});
});
</script>
<form action="" method="post" name="form_spa_inv">
<fieldset class="row3">
<table>
<tr>
<td><input type="button" value="Add Treatment" id="addRow" /></td>
<td><input type="button" value="Remove Treatment" id="deleteRow" /></td>
</tr>
</table>
<table id="dataTable" class="form" border="1">
<tr>
<td></td>
<td>No</td>
<td>Description</td>
<td>Qty</td>
<td>Unit Price</td>
<td>Amount</td>
</tr>
</table>
<!-- table row template here -->
<table id="row_template" style="display:none">
<tr>
<td><input type="checkbox" name="chk[]" class="mychkbox" /></td>
<td class="tablerow"></td>
<td>
<select name="order_desc[]" id="order_desc" class="select-desc">
<option value="">-- Select Treatment --</option>
<?php
while($dd=mysql_fetch_array($gettminfo)) {
?>
<option value="<?php echo $dd['tm_unit_prc']?>"><?php echo $dd['tm_abb'] ?> - <?php echo $dd['tm_desc'] ?></option>
<?php
}
?>
</select>
</td>
<td>
<input type="text" name="order_qty[]" id="order_qty" size="10">
</td>
<td>
<input type="text" name="order_unit_prc[]" id="order_unit_prc" readonly>
</td>
<td>
<input type="text" name="order_amt[]" id="order_amt" readonly>
</td>
</tr>
</table>
</fieldset>
<p style="text-align: center;"><input type="submit" name="Submit" value="Create">
</form>
</body>
</html>
Insert mysql query
<?php
//including the database connection file
include_once("config_db.php");
if(isset($_POST['Submit']))
{
for($i=0; $i < count($_POST['order_desc']); $i++) {
$order_desc = addslashes($_POST['order_desc'][$i]);
$order_qty = addslashes($_POST['order_qty'][$i]);
$order_unit_prc = addslashes($_POST['order_unit_prc'][$i]);
$order_amt = addslashes($_POST['order_amt'][$i]);
//insert data to database tb_odr_dtl
$insert_odrdtl=mysql_query("INSERT INTO tb_odr_dtl(order_desc,order_qty,order_unit_prc,order_amt) VALUES('$order_desc','$order_qty','$order_unit_prc','$order_amt')");
mysql_query($insert_odrdtl);
//display success message
if($insert_odrdtl) {
echo "<script>alert('Invoice created successfully!')</script>";
echo "<script>window.open('cv.php','_self')</script>";
}
}
}
?>
Change server site code to following:
<?php
//including the database connection file
include_once("config_db.php");
if(isset($_POST['Submit']))
{
for($i=0; $i < count($_POST['order_desc']); $i++) {
$order_desc = addslashes($_POST['order_desc'][$i]);
$order_qty = addslashes($_POST['order_qty'][$i]);
$order_unit_prc = addslashes($_POST['order_unit_prc'][$i]);
$order_amt = addslashes($_POST['order_amt'][$i]);
//insert data to database tb_odr_dtl
$insert_odrdtl=mysql_query("INSERT INTO tb_odr_dtl(order_desc,order_qty,order_unit_prc,order_amt) VALUES('$order_desc','$order_qty','$order_unit_prc','$order_amt')");
//mysql_query($insert_odrdtl); //delete this line of code
//display success message
if($insert_odrdtl) {
echo "<script>alert('Invoice created successfully!')</script>";
echo "<script>window.open('cv.php','_self')</script>";
}
}
}
?>
I'm trying to setup a form that can update my product.
the code reads data ok, but $update is getting errors that prevents the update from doing anything.
The errors are :
Undefined variable: update
mysqli::query(): Empty query (after submit the form)
Please Help! Thanks.
//include database configuration file
include("config.php");
$mysqli->set_charset("utf8");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Edit Page</title>
</head>
<body>
<?php
if(isset($_POST['Submit'])){//if the submit button is clicked
$updateproductname = $_POST['updateproductname'];
$updatesku = $_POST['productsku'];
$updateproductoriginal = $_POST['updateoriginalname'];
$updatedescshort = $_POST['updatedescshort'];
$update = $mysqli->query("UPDATE testproducts".
"SET product_sku=$updatesku, product_name=$updateproductname, 'product_originalname'='$updateproductoriginal', 'product_description_short='$updatedescshort' ".
"WHERE product_id = '$id' ");
$mysqli->query($update) or die("Cannot update");//update or error
}
?>
<?php
//Create a query
$sql = "SELECT * FROM testproducts WHERE product_id = $id";
//submit the query and capture the result
$result = $mysqli->query($sql) or die(mysql_error());
?>
<h2>Update Record <?php echo $id;?></h2>
<form action="" method="post">
<?php
while ($row = $result->fetch_assoc()) {?>
<table border="0" cellspacing="10">
<tr>
<td>Product Name:</td> <td><input type="text" name="updateproductname" value="<?php echo $row['product_name']; ?>"></td>
</tr>
<tr>
<td>Product Original Name:</td> <td><input type="text" name="updateoriginalname" value="<?php echo $row['product_originalname']; ?>"></td>
</tr>
<tr>
<td>Product SKU:</td> <td><input type="text" name="productsku" value="<?php echo $row['product_sku']; ?>"></td>
</tr>
<tr>
<td>ShortDescription:</td> <td><input type="text" name="updatedescshort" size="100" value="<?php echo $row['product_description_short']; ?>"></td>
</tr>
<tr>
<td><INPUT TYPE="Submit" VALUE="Update the Record" NAME="Submit"></td>
</tr>
</table>
<?php
}
?>
</form>
<?php
if($update){//if the update worked
echo "<b>Update successful!</b>";
}
?>
</body>
</html>
a) You are vulnerable to SQL injection attacks
b) Read the docs for mysqli_query(). The function takes a query STRING, and returns a RESULT HANDLE. You're then taking that result handle and trying to re-query it. If you'd bothered having proper error handling on ALL of your mysqli calls, you'd have seen this.
was able to update the record after moving the update and select code to top of html
<?php
if(isset($_POST['Submit'])){//if the submit button is clicked
// Check connection
$productname = $_POST['updateproductname'];
$productoriginal = $_POST['updateoriginalname'];
$sku = $_POST['productsku'];
$descshort = $_POST['updatedescshort'];
$mysqli->query("UPDATE testproducts ".
"SET product_name='$productname',product_originalname='$productoriginal', product_sku='$sku', product_description_short='$descshort'".
" WHERE product_id='$id'");
}
?>
<?php
//Create a query
$sql = "SELECT * FROM testproducts WHERE product_id = $id";
//submit the query and capture the result
$result = $mysqli->query($sql) or die(mysql_error());
//$query=getenv(QUERY_STRING);
//parse_str($query);
//$ud_title = $_POST['Title'];
//$ud_pub = $_POST['Publisher'];
//$ud_pubdate = $_POST['PublishDate'];
//$ud_img = $_POST['Image'];
$mysqli->close();
?>
Assalamu'alaikum, i am use this scripts for calculate the value of checkbox named "Biaya" and it is works.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
</style>
</head>
<body>
<?php
mysql_connect("localhost", "root")or die("cannot connect");
mysql_select_db("spp")or die("cannot select DB");
$sql="SELECT `idtagihan`, `namatagihan`,`biaya` from tagihan";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<table border=1>
<tr>
<td>
<form name="form1" method="post">
<table>
<tr>
<td>Id</td>
<td>Nama</td>
<td>Harga</td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $rows['idtagihan']; ?>></td>
<td><?php echo $rows['namatagihan']; ?></td>
<td>Rp. <?php echo $rows['biaya']; ?>,-</td>
<td><input type="checkbox" name=check[] value="<?php echo $rows['namatagihan']; ?>" data-weight="<?php echo $rows['biaya']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan=3><input name="Next" type="submit" id="Next" value="Next"></td>
</tr>
<?php
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
<div>Total: <span id="total">0</span></div>
</body>
</html>
<script type="text/javascript">
(function () {
var totalEl = document.getElementById('total'),
total = 0,
checkboxes = document.form1['check[]'],
handleClick = function () {
total += parseInt(this.getAttribute('data-weight'), 10) * (this.checked ? 1 : -1);
totalEl.innerHTML = total;
},
i, l
;
for (i = 0, l = checkboxes.length; i < l; ++i) {
checkboxes[i].onclick = handleClick;
}
}());
</script>
The result is like this :
First result pic
Then i develop that code for updating it's value to database but it cannot calculate anymore. Here is the whole php code :
<?php
// Start session
session_start();
require_once('includes/functions.inc.php');
// Check login status... if not logged in, redirect to login screen
if (check_login_status() == false) {
redirect('login.php');
}
// Connection to the database
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="spp"; // Database name
$tbl_name="tagihan"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
if(isset($_POST['check'])){$checkbox = $_POST['check'];
if(isset($_POST['activate'])?$activate = $_POST["activate"]:$deactivate = $_POST["deactivate"])
$id = "('" . implode( "','", $checkbox ) . "');" ;
$sql2="UPDATE tagihan SET status = '".(isset($activate)?'Lunas':'Belum Lunas')."' WHERE idtagihan IN $id" ;
$result = mysql_query($sql2) or die(mysql_error());
}
$nim = $_SESSION['nim'];
$sql="SELECT `idtagihan`, `namatagihan`, `biaya` FROM tagihan WHERE nim='".$nim."' and status='Belum lunas' ";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-type" content="text/html;charset=utf-8" />
<title>Pembayaran SPP</title>
<link rel="stylesheet" type="text/css" href="css/gaya.css" />
<link href="css/menu.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="halaman">
<h1>Pembayaran SPP</h1>
<p>Keluar
<?php
$nim = $_SESSION['nim'];
?>
<label>Nim anda : </label> <input type="total" name="nim" id="nim" readOnly="readonly" value="<?php echo $nim; ?>" >
</p>
</br>
<div class="tabel" >
<form name="frmactive" method="post" action="">
<table>
<tr>
<td>Id</td>
<td>Nama</td>
<td>Harga</td>
<td>Pilih</td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $rows['idtagihan']; ?></td>
<td><?php echo $rows['namatagihan']; ?></td>
<td>Rp. <?php echo $rows['biaya']; ?>,-</td>
<td><input class="css-checkbox" type="checkbox" name='check[]' name='cek[]' value="<?php echo $rows['idtagihan']; ?>" biaya = "<?php echo $rows['biaya']; ?>"></td>
</tr>
<?php
}
?> </table>
<div class="tabelspp" >
<table>
<tr>
<td><label>Total Rp.</label><input type="total" id="total2" readOnly="readonly"></input><label>,-</label>
<input type="submit" name="activate" id="activate" value="Bayar" /> </td>
</tr>
</table> </form> </div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
<script type="text/javascript">
(function () {
var totalEl = document.getElementById('total2'),
total2 = 0,
checkboxes = document.form1['check[]'],
handleClick = function () {
total2 += parseInt(this.getAttribute('biaya'), 10) * (this.checked ? 1 : -1);
totalEl.value = total2;
},
i, l
;
for (i = 0, l = checkboxes.length; i < l; ++i) {
checkboxes[i].onclick = handleClick;
}
}());
</script>
Then here is the result :
Second result pic
I can update the database successfully. But my question is how to make that second script can calculate the value's checkbox again same as first script? I need to resolve this, i hope there someone who can answer this. I am happy if there anyone reply my post. Thank you.
for first, try to change the name of the form
from
<form name="frmactive" method="post" action="">
to this
<form method="post" action="" name="form1">
Warning this is lenghty! attack if you knowledagble. well at least more then a newb beginner like me.
This script uses three files as detailed below. It is suppoed to create the database and fields from the form input. It gets to the end and shows my_contacts has been created!. But when i go into phpMyadmin the table has not been created.
I have a file named show_createtable.html which is used to create a table in MySQL
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<h1>Step 1: Name and Number</h1>
<form method="post" action="do_showfielddef.php" />
<p><strong>Table Name:</strong><br />
<input type="text" name="table_name" size="30" /></p>
<p><strong>Number of fields:</strong><br />
<input type="text" name="num_fields" size="30" /></p>
<p><input type="submit" name="submit" value="go to step2" /></p>
</form>
</body>
</html>
This Form Posts to do_showfielddef.php
<?php
//validate important input
if ((!$_POST[table_name]) || (!$_POST[num_fields])) {
header( "location: show_createtable.html");
exit;
}
//begin creating form for display
$form_block = "
<form action=\"do_createtable.php\" method=\"post\">
<input name=\"table_name\" type=\"hidden\" value=\"$_POST[table_name]\">
<table cellspacing=\"5\" cellpadding=\"5\">
<tr>
<th>Field Name</th><th>Field Type</th><th>Table Length</th><th>Primary Key?</th><th>Auto-Increment?</th>
</tr>";
//count from 0 until you reach the number fo fields
for ($i = 0; $i <$_POST[num_fields]; $i++) {
$form_block .="
<tr>
<td align=center><input type=\"texr\" name=\"field name[]\"
size=\"30\"></td>
<td align=center>
<select name=\"field_type[]\">
<option value=\"char\">char</option>
<option value=\"date\">date</option>
<option value=\"float\">float</option>
<option value=\"int\">int</option>
<option value=\"text\">text</option>
<option value=\"varchar\">varchar</option>
</select>
</td>
<td align=center><input type=\"text\" name=\"field_length[]\" size=\"5\"></td>
<td aligh=center><input type=\"checkbox\" name=\"primary[]\" value=\"Y\"></td>
<td aligh=center><input type=\"checkbox\" name=\"auto_increment[]\" value=\"Y\"></td>
</tr>";
}
//finish up the form
$form_block .= "
<tr>
<td align=center colspan=3><input type =\"submit\" value=\"create table\">
</td>
</tr>
</table>
</form>";
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Create a database table: Step 2</title>
</head>
<body>
<h1>defnie fields for <? echo "$_POST[table_name]"; ?>
</h1>
<? echo "$form_block"; ?>
</body>
</html>
Which in turn creates the table and fields with this file do_showfielddef.php
//connect to database
$connection = #mysql_connect("localhost", "user", "pass")
or die(mysql_error());
$db = #mysql_select_db($db_name, $connection)
or die(mysql_error());
//start creating the SQL statement
$sql = "CREATE TABLE $_POST[table_name](";
//continue the SQL statement for each new field
for ($i = 0; $i < count($_POST[field_name]); $i++) {
$sql .= $_POST[field_name][$i]." ".$_POST[field_type][$i];
if ($_POST[auto_increment][$i] =="Y") {
$additional = "NOT NULL auto_increment";
} else {
$additional = "";
}
if ($_POST[primary][$i] =="Y") {
$additional .= ", primary key (".$_POST[field_name][$i].")";
} else {
$additional = "";
}
if ($_POST[field_length][$i] !="") {
$sql .= " (".$_POST[field_length][$i].") $additional ,";
} else {
$sql .=" $additional ,";
}
}
//clean up the end of the string
$sql = substr($sql, 0, -1);
$sql .= ")";
//execute the query
$result = mysql_query($sql, $connection) or die(mysql_error());
//get a giid message for display upon success
if ($result) {
$msg = "<p>" .$_POST[table_name]." has been created!</p>";
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Create A Database Table: Step 3</title>
</head>
<body>
<h1>Adding table to <? echo "$db_name"; ?>...</h1>
<? echo "$msg"; ?>
</body>
</html>
I cant believe I went to all the trouble of wrinting this Question. I had another good look at the phpMYAdmin and it had worked. The table had been created under a database called testDB which I assumed had nothing it in.
How did the script decided to etner this as a child under the testDB database?
Once again thanks everyone for your input, This site is truely amazine and is so valuable for a beginner like my self.