I have 3 files
1) show_createtable.html
2) do_showfielddef.php
3) do_showtble.php
1) First file is for creating a new table for a data base, it is a fom with 2 inputs, Table Name and Number of Fields. THIS WORKS FINE!
<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>
2) this script validates fields and createa another form to enter all the table rows.
This for also WORKS FINE!
<?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>
</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>
</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>
Problem is here
3) this form creates the tables and enteres them into the database.
I am getting an error on line 37 "Parse error: syntax error, unexpected $end in /home/admin/domains/domaina.com.au/public_html/do_createtable.php on line 37"
<?
$db_name = "testDB";
$connection = #mysql_connect("localhost", "admin_user", "pass")
or die(mysql_error());
$db = #mysql_select_db($db_name, $connection)
or die(mysql_error());
$sql = "CREATE TABLE $_POST[table_name](";
for ($i = 0; $i < count($_POST[field_name]); $i++) {
$sql .= $_POST[field_name][$i]." ".$_POST[field_type][$i];
if ($_POST[field_length][$i] !="") {
$sql .=" (".$_POST[field_length][$i]."),";
} else {
$sql .=",";
}
$sql = substr($sql, 0, -1);
$sql .= ")";
$result = mysql_query($sql, $connection) or die(mysql_error());
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>
$result = mysql_query($sql, $connection) or die(mysql_error());
if ($result) {
$msg = "<p>" .$_POST[table_name]." has been created!</p>";
}
you missing a } in your last if statement, and your for loop is missing a } too
for ($i = 0; $i < count($_POST[field_name]); $i++) {
$sql .= $_POST[field_name][$i]." ".$_POST[field_type][$i];
if ($_POST[field_length][$i] !="") {
$sql .=" (".$_POST[field_length][$i]."),";
} else {
$sql .=",";
}
}
This error message means that a control structure block isn’t closed properly. In your case the closing } of some of your control structures like the for loop or the last if are missing.
You should use proper indentation and an editor that highlights bracket pairs to have a visual aid to avoid such errors.
You must close the for expression block:
for ($i = 0; $i < count($_POST[field_name]); $i++) {
$sql .= $_POST[field_name][$i]." ".$_POST[field_type][$i];
// ...
}
Your for loop is not terminated. You are missing a }
for ($i = 0; $i < count($_POST[field_name]); $i++) {
$sql .= $_POST[field_name][$i]." ".$_POST[field_type][$i];
}
And as pointed by others there is also a missing } for the last if statement:
if ($result) {
$msg = "< p>" .$_POST[table_name]." has been created!< /p>";
}
in your php.ini (php configuration) change :
short_open_tag = Off
you opened php tag shortly at line 1
just find and replace all <? with <?php
Stylistic tip: Use HEREDOCs to assign blocks of text to a variable, instead of the hideous multi-line-with-tons-of-escaping-backslashes constructs you're using. They're far easier to read and less error prone if/when you happen to forget a \ somewhere and break the script with a parse error.
I just solved this error, after checking my code, I had no open tags/braces.
For me, I got this error when moving to a amazon server.
It turns out I needed to enable short_open_tag = On in my php.ini.
This solved this error for me.
Related
Im becomming quite a regular on here...
I am trying to dynamicly print out a table in PHP depending on what results are found with a MYSQL Statement.
See the below code, I am getting the below error
[Fri Jun 09 18:51:32.478737 2017] [fcgid:warn] [pid 63368] [client 5.69.190.95:64631] mod_fcgid: stderr: PHP Parse error: syntax error, unexpected 'showhistory' (T_STRING), expecting ',' or ';' in /home/tools/public_html/searchhistory.php on line 84, referer: http://tools.cidetech.co.uk/history.php
It seems to have a problem with just the "form" building inside of the loop I have no problems up until this line -
echo " <td><form method="POST" action="showhistory.php">
<input type="hidden" name="id_director" value=".$row["id"]"
</form></td> ";
I cant seem to figure out where I am going wrong, this would work fine in just pure HTML, however it needs to be inside of the mysql/php part as i need to pass the row id through inside of the button.
To be specific it is this part of the code I am struggling with
for ($i = 0; $i < count($idArray); $i++)
{
$sql="SELECT * FROM history WHERE id LIKE '%{$idArray[$i]}%'";
$result=$con->query($sql);
while($row=$result->fetch_assoc())
{
echo "<tr>";
echo "<td><pre>".$row["id"]."</pre></td>";
echo "<td><pre>".$row["date"]."</pre></td>";
echo "<td><pre>".$row["domain"]."</pre></td>";
echo " <td><form method="POST" action="showhistory.php">
<input type="hidden" name="id_director" value=".$row["id"]"
</form></td> ";
}
}
echo "</table>";
mysqli_close($conn);
?>
The full code can be seen here
<DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<meta name="generator" content="PSPad editor, www.pspad.com">
<title>CWCS Domain Checker Tool</title>
</head>
<body>
<div class="header">
<a href="index.php">
<img src="cwcs-logo.png">
</a>
</div>
<hr/>
<div class="searchform">
<form action="searchhistory.php" method="post">
<label for="domain"> <input class="submit" type="text" name="domain" /> </label>
<input class="submitbutton" type="submit" name="search" value="Search for Domain" />
</form>
</div>
<?php
#define connection info/variables needed
$servername = "localhost";
$username = "";
$password = "";
$dbname = "domainhistory";
$domain = $_POST['domain'];
$idArray = array();
#creates mysql connection
$con=new mysqli($servername,$username,$password,$dbname);
if($con->connect_error)
{
echo 'Connection Faild: '.$con->connect_error;
}
else
{
$sql="SELECT * FROM history WHERE domain LIKE '%{$domain}%'";
$result=$con->query($sql);
#Pushes the ID of the mysql row into an array
while($row=$result->fetch_assoc())
{
array_push($idArray,$row["id"]);
}
}
mysqli_close($conn);
?>
<!---prints out the ID's stored in the array -->
<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "domainhistory";
$con=new mysqli($servername,$username,$password,$dbname);
if($con->connect_error)
{
echo 'Connection Faild: '.$con->connect_error;
}
else
{
}
echo "<table>";
echo "<tr>";
echo "<th> ID </th>";
echo "<th> Domain</th>";
echo "<th> Date </th>";
echo "</tr>";
## - loops through the ID array, and then prints out the data relating to that ID.
for ($i = 0; $i < count($idArray); $i++)
{
$sql="SELECT * FROM history WHERE id LIKE '%{$idArray[$i]}%'";
$result=$con->query($sql);
while($row=$result->fetch_assoc())
{
echo "<tr>";
echo "<td><pre>".$row["id"]."</pre></td>";
echo "<td><pre>".$row["date"]."</pre></td>";
echo "<td><pre>".$row["domain"]."</pre></td>";
echo " <td><form method="POST" action="showhistory.php">
<input type="hidden" name="id_director" value=".$row["id"]"
</form></td> ";
}
}
echo "</table>";
mysqli_close($conn);
?>
</body>
</html>
You should use ' instead of " for your string in php, because you have the " used in the html markup and the " you use for echo.
I've tried a million different ways, and this is probably something super simple, but I can't get it figured out.
I have a database that displays several different columns (name, age, gender, breed, player, etc.)
What I would like is to include a line that states "We have ___ number of female characters."
Here is my entire page where I want it displayed:
<!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>
<title>AUSUS --- mythical equine RPG</title>
<link rel="stylesheet" type="text/css" href="/css/evolution2.css" />
<link href='http://fonts.googleapis.com/css?family=Vollkorn:400,400italic,700,700italic|Kreon:400,700,300|La+Belle+Aurore|Stalemate|IM+Fell+English:400,400italic|Mrs+Saint+Delafield' rel='stylesheet' type='text/css'>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>Character Database</h1>
<p>
We currently have <b>
<?php
$link = mysql_connect("***", "***", "***");
mysql_select_db("tarb89_characters", $link);
$result = mysql_query("SELECT * FROM characters", $link);
$num_rows = mysql_num_rows($result);
echo "$num_rows";
?>
</b>
characters listed in the database. You may search the character database by character name or player name. Dams and sires marked with an asterisk (*) are from <b>outside</b> Ausus.<br><br>
<br><br>
<form action="searchchars.php" method="GET"/>
<input type="text" name="searchchars" />
<input type="submit" name="submit" value="Character Name Search" /><br>
</form>
<br>
<form action="searchplays.php" method="GET"/>
<input type="text" name="searchplays" />
<input type="submit" name="submit" value="Player Name Search" />
</form>
</p>
<p>
<br><br>
<?php
/// In order to use this script freely
/// you must leave the following copyright
/// information in this file:
/// Copyright 2012 www.turningturnip.co.uk
/// All rights reserved.
include("connect.php");
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 20;
$result = mysql_query("SELECT * FROM characters ORDER BY name ASC LIMIT $start_from, 20");
$num = mysql_num_rows ($result);
if ($num > 0 ) {
$i=0;
while ($i < $num) {
$id = stripslashes(mysql_result($result,$i,"id"));
$name = stripslashes(mysql_result($result,$i,"name"));
$breed = stripslashes(mysql_result($result,$i,"breed"));
$gender = stripslashes(mysql_result($result,$i,"gender"));
$color = stripslashes(mysql_result($result,$i,"color"));
$markings = stripslashes(mysql_result($result,$i,"markings"));
$genetics = stripslashes(mysql_result($result,$i,"genetics"));
$player = stripslashes(mysql_result($result,$i,"player"));
$traits = stripslashes(mysql_result($result,$i,"traits"));
$defects = stripslashes(mysql_result($result,$i,"defects"));
$extras = stripslashes(mysql_result($result,$i,"extras"));
$profile = stripslashes(mysql_result($result,$i,"profile"));
$dam = stripslashes(mysql_result($result,$i,"dam"));
$sire = stripslashes(mysql_result($result,$i,"sire"));
$status = stripslashes(mysql_result($result,$i,"status"));
$row .= '
<h5>'.$name.'</h5>
<table width="550px" cellpadding="5" cellspacing="20" style="background: #eeeeee; border-radius: 15px; border: 1px solid #5067be;">
<tr>
<td width="50%"><p>
<b>ID Number:</b> '.$id.'<br>
<b>Breed:</b> '.$breed.'<br>
<b>Gender:</b> '.$gender.'<br>
<b>Dam: </b>'.$dam.'<br>
<b>Sire: </b>'.$sire.'<br>
<b>Status: </b>'.$status.'<br>
<b>Profile:</b> Click Here<br>
<b>Player:</b> '.$player.'</p></td>
<td width="50%"><p>
<b>Color:</b> '.$color.'<br>
<b>Genetics: </b>'.$genetics.'<br>
<b>Markings:</b> '.$markings.'<br>
<b>Traits:</b> '.$traits.'<br>
<b>Defects:</b> '.$defects.'<br>
<b>Extras:</b> '.$extras.'</p></td>
</table><br>
';
++$i; }} else { $row = '<tr><td colspan="2" align="center">Database empty.</td></tr>'; }
mysql_close();
?>
<? echo $row ?>
</p><center>
<font face="tahoma" style="font-size: 12px;">Pages:
<?php
include("connect.php");
$result = mysql_query("SELECT COUNT(name) FROM characters");
$row = mysql_fetch_row($result);
$total_records = $row[0];
$total_pages = ceil($total_records / 20);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='index.php?page=".$i."'>".$i."</a> | ";
};
?>
</center></font>
</body>
</html>
Here is example for the same you can edit this
<?php
$cnt = 0;
$query = "select * from table";
$resulr = mysql_query($query);
while($var = mysql_fetch_array($result)){
if($var['gender']=='female'){
$cnt++;
}
}
echo "We have ".$cnt." female characters";
?>
SELECT COUNT(id) FROM characters where gender = 'female'
That will get you the count of characters which have 'female' as the value for gender. Is that what you want?
I am just building a simple search page in PHP. I need to know how can i keep the selecte value of the drop down list upon form submission. Currently, the value resets to the first index.
Can I do this via PHP without using client-side script?
Here is the code:
<?php
mysql_connect('localhost','root','');
mysql_select_db('hotel');
$sql = "SELECT * FROM customers";
$result = mysql_query($sql);
?>
<!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>
<form method="get">
<select name="field" id="field">
<?php
/*if($field == 'Active')
'selected="selected"';
*/
while($rows = mysql_fetch_array($result))
echo '<option>'.$rows['customer_id'].'</option><br>';
?>
</select>
<?php
if (isset($_GET['Search']) && $_GET['action']=='search')
{
$sql="SELECT * FROM customers WHERE customer_id=".$_GET['field'];
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
echo '<br>Customer Name: '.$row['customer_name'].'<br>';
echo 'Email Address: '.$row['Email_Addr'].'<br>';
echo 'Contact No: '.$row['Contact_No'].'<br>';
}
?>
<input type="hidden" name="action" value="search" />
<br><input type="submit" value="search" name="Search" onclick="" />
</form>
</body>
</html>
usually like this.
echo '<option';
if ($_GET['field'] == $rows['customer_id']) echo " selected";
echo '>'.$rows['customer_id'].'</option>';
And please don't use the mysql_* functions to write new code, especially when you are learning. The mysql_* functions are in the process of becoming deprecated, they will be removed in future versions of PHP. Use mysqli_* or PDO objects instead.
You can check if the get value is the same than the select value :
while($rows = mysql_fetch_array($result))
echo '<option value="'.$rows['customer_id'].'" '.($rows['customer_id'] == $_GET['field']?'selected="selected"':'').'>'.$rows['customer_id'].'</option><br>';
When printing the select options, you can check for the value and set any mathing option to selected, maybe like this:
while($rows = mysql_fetch_array($result)){
if(!empty($_GET['field']) && $_GET['field'] == $rows['customer_id']){
echo '<option selected="selected">'.$rows['customer_id'].'</option><br>';
}
else {
echo '<option>'.$rows['customer_id'].'</option><br>';
}
}
Hi I have a page that displays all of the contents of my table. Alongisde each row of the table I also have a column containing a checkbox. When the user selects one or more rows by ticking the checkbox and pressing the submit button, I want just those rows to appear in a table on the next page (buy.php). I know it is basically a select statement per row that is selected. But I dont know how to approach this. Can anybody help? Thanks
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Shopping</title>
</head>
<body>
<form action='buy.php' method='post'>
<input type='submit' value='Submit' />
</form>
<h1>Buy</h1>
<?php // Script 12.7 - sopping.php
$db = mysql_connect('localhost', '#####', '#####');
mysql_select_db('shopping', $db);
$query = 'SELECT * FROM Items';
if ($r = mysql_query($query, $db)) {
print "<form>
<table>";
while ($row = mysql_fetch_array($r)) {
print
"<tr>
<td>{$row['ID']}</td>
<td>{$row['Name']}</td>
<td>{$row['Cost']}</td>
<td><input type='checkbox' name='buy[{$row['ID']}] value='buy' /></td>
</tr>";
}
print "</table>
</form>";
} else {
print '<p style="color: blue">Error!</p>';
}
mysql_close($db); // Close the connection.
?>
</body>
</html>
EDIT: I tried the suggestion below and I did not get a table. Just the title of the page appeard:
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Confirmation</title>
</head>
<body>
<h1>Order Details</h1>
<?php // Script 12.7 - buy.php
$db = mysql_connect('localhost', '#####', '#####');
mysql_select_db('shopping', $db);
foreach($_POST['buy'] as $item) {
$query = 'SELECT * FROM Items WHERE ID = $item';
if ($r = mysql_query($query, $db)) {
print "<table>";
while ($row = mysql_fetch_array($r)) {
print
"<tr>
<td>{$row['ID']}</td>
<td>{$row['Name']}</td>
<td>{$row['Cost']}</td>
</tr>";
}
print "</table>";
} else {
print '<p style="color: blue">Error!</p>';
}
}
mysql_close($db);
?>
</body>
</html>
You need to create a dynamic SQL query.
1) I suggest you change your checkbox to the following
<input type='checkbox' name='buy[]' value='{$row['ID']}' />
2) Loop through all of the buy options
<?php
foreach($_POST['buy'] as $item) {
// Append the ID (in the $item variable) to the SQL query, using WHERE `ID` = $item OR `ID` = $item and so on
}
?>
UPDATE:
<?php // Script 12.7 - buy.php
$db = mysql_connect('localhost', '#####', '#####');
mysql_select_db('shopping', $db);
$query = 'SELECT * FROM Items WHERE ';
$item_count = count($_POST['buy']);
for($i = 0; $i < $item_count; $i++) {
$itemid = (int)mysql_real_escape_string($_POST['buy'][i]); // Secures It
$query .= '`ID` = '.$itemid;
if($i +1 < $item_count) {
$query .= ' OR ';
}
}
if ($r = mysql_query($query, $db)) {
print "<table>";
while ($row = mysql_fetch_array($r)) {
print
"<tr>
<td>{$row['ID']}</td>
<td>{$row['Name']}</td>
<td>{$row['Cost']}</td>
</tr>";
}
print "</table>";
mysql_close($db);
?>
// this checkbox use
//buy.php foreach($_POST['buy'] as $item) {
$query = "SELECT * FROM Items WHERE ID = $item"; //use " "
// try it if any problem please carefully look you database table // download file and try it 2file with table sql data
// https://copy.com/8ZsBAFj7LvypLJkK
// this checkbox use
<input type='checkbox' name='buy[]' value='{$row['ID']}' />
//buy.php
foreach($_POST['buy'] as $item) {
$query = "SELECT * FROM Items WHERE ID = $item";
//use " "
// try it if any problem please carefully look you database table
// download file and try it 2file with table sql data
// https://copy.com/8ZsBAFj7LvypLJkK
I have made database that contains some information. Want to do is to retrieve and display data that fits with parameters like:
if i select green from dropdown list, it will display only information about cars that are green. Also "kõik" means like all.
Here is form that im using.
<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 align="center" style="margin-top:200px;">
<table border="1" align="center">
<tr>
<td>
<form action="retrieve.php" method="get">
Rass:
<select name="rass">
<option value="ebony">Ebony</option>
<option value="Valge">Valge</option>
<option value="Aasia">Aasia</option>
</select> <br />
<input type="submit" value="lisa" />
</form>
</td>
</tr>
</table>
</body>
</html>
Here is php that used to show all data.
<?php
$connect = #mysql_connect ("localhost", "root", "") or die("Fail!!!! :D:D:D");
mysql_select_db("tibid") or die("selline andmebaas puudub");
$query = mysql_query("select * from test");
$num_rows = mysql_num_rows($query);
if($num_rows > 0){
{while($row = mysql_fetch_assoc($query))
echo
$row['rinnad']. "<br>"
.$row['juuksed']."<br>"
.$row['silmad']."<br>"
.$row['rass']."<br>
<hr>
";}
}else{
echo "Andmebaas on tühi";
}
?>
Link to this kind tutorial would be helpful.
Thank you
I think you are looking for WHERE. So your query would be:
select * from cars where color = '$color'
where $color is escaped $_POST['color'] passed from your form.