Adding new columns to a table using php/sql - php

I'm trying to create a feature that allows me to add a new column to a table in my database using sql and php. I currently have a front end form, where the user can select what table they wish to alter and input boxes for adding a new column name. I also want to display the new version of the selected table (i.e. with the new column attached) My front and back end scripts look like this below.
sql10.html:
<form action="sql10.php" method="post">
<select id="category">
<option value="" disabled selected>Select your option</option>
<option id="customer" value="customer">Customer Table</option>
<option id="booking "value="booking">Booking Table</option>
<option id="bookingline" value="bookingline">Order Table</option>
<option id="package" value="package">Package Table</option>
</select>
<br><br>
Column Name: <input type="text" name="colname" placeholder="Insert New Column Name">
<br>
<input type="submit" value="Submit"> <input type="reset" value="Clear">
sql.php:
<?php
$conn = mysqli_connect("localhost", "######", "#####")
or die("Could not connect: " . mysqli_error($conn));
print "Successful Connection!";
mysqli_select_db($conn, '#####') or die ('db will not open' );
print " You have connected to the Assignment 3 database.<br>";
$category = $_POST["category"];
$newcolumn = $_POST["colname"];
$query1="ALTER TABLE $category ADD $newcolumn varchar(255)";
$query2 = "SELECT * FROM $category";
mysqli_query($conn, $query1) or die ("Invalid query");
echo "Success in database entry";
$numrows = mysqli_affected_rows($conn);
echo $numrows . "row updated<br>";
$result = mysqli_query($conn, $query) or die("Invalid query");
$num = mysqli_num_rows($result);
echo "<table border='1'><tr><th>Booking ID</th></tr>";
for($i=0; $i<$num; $i++) { //uses $num as loop end value
$row = mysqli_fetch_row($result);
echo "<tr><td>" . $row[0] . "</td></tr>";
}
echo "</table>";
mysqli_close($conn);
?>
As of using this code, I am not able to create a new column for tables. My database consists of multiple tables and I want the user to be able to select which table they wish to add a column to as well as enter the name of the new column. Can anybody see what the problem is?

I think your mistake is that your select tag doesn't have a name attribute. Remember that a form when is submitted, it takes the value of elements with the name attribute.
So, if you put in your HTML code <select id="category" name="category">, you will be able to catch its value in your sql.php query with $_POST["category"]; maybe the $_POST["category"] is arriving empty, and therefore you cannot execute the $query1 sentence.

Related

How to fetch data from database table on PHP page

I have created one IMS in that I am trying to fetch data from one table from the database and display it in drop-down form to another page.
In the database, I have created one table named a party in that table one column named party_name is available and I have to fetch that column data to my order page. Below is my Code. If anyone knows a solution than please help.
<select name="party[]" style="width:100%;" required>
<?php
$result=mysql_query("SELECT * FROM partys");
while ($row = mysql_fetch_array($result)) {
?>
<option value="<?php echo $row['id'];?>"><?php echo $row['party_name'];?></option>
<?php
}
?>
</select>
Firstly: mysql extension is deprecated, you should use at least mysqli_*:
Secondly: try the below example, replacing the database connection string variables with your database credentials:
<select name="party[]" style="width:100%;" required>
<option value="">-- Please select --</option>
<?php
$dbc = mysqli_connect('localhost', 'userName', 'password', 'databaseName')
or die('Error connecting to MySQL server.');
$query = "SELECT * FROM partys";
$result = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $row['id'];?>"><?php echo $row['party_name'];?></option>
<?php } ?>
</select>

Fill drop down list on page load php

I have two input text fields where user has to specify the begin and end of the fly.
<input type="text" name="start" placeholder="Start destination">
<input type="text" name="end" placeholder="End destination">
I would like to change that and give user to chose start and end destination from database.
<select>
<option value="$id">$name</option>
</select>
I know how to get done if i read database and input values manually, but i know its posible if page loads and execute my SELECT QUERY.
So i have to create dropdown list and fill that with a values from database.
This dropdown list has to be filled when the page load.
Some idea for this ???
I am working with php.
Thank you in advance !!
EDIT : I get done this only with php.
<?php
$db_host = "localhost";
$db_username = "root";
$db_password = "";
$db_name = "flights";
$conn = mysql_connect("$db_host","$db_username","$db_password") or die ("no conn");
#mysql_select_db("$db_name") or die ("no database");
if ($conn = true) {
// echo "";
}
//cyrilic
$sql = "SET NAMES 'utf8'";
mysql_query($sql);
//query for end
$sql="SELECT Distinct end from flights_table;";
$result=mysql_query($sql);
echo "<select name=\"city\">";
echo "<option>end destination</option>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='".$row['end']."'>".$row['end']." </option>";
}
echo "</select>";
?>
This php fires when page loads. Those select options i have putted in a form, and when form is submited, it fires php itself. I am getting selected options this way :
$startfly=$_POST['end'];
I am doing this for starting the flight :)
Thank you guys !
Try this :
At the top of page include your database connection file :
<?php
require "connection.php";
?>
Then :
<?php
$selectStart = "Start : <select name='start'>";
$selectEnd = "End : <select name='end'>";
$query = mysql_query("SELECT * FROM someTable ORDER BY dateField ASC");
if(mysql_num_rows($query) > 0)
{
while($row = mysql_fetch_assoc($query))
{
$selectStart .= "<option value='".$row['startItem']."'>".$row['startItemName']."</option>";
$selectEnd .= "<option value='".$row['endItem']."'>".$row['endItemName']."</option>";
}
}
$selectStart = "</select>";
$selectEnd = "</select>";
?>
In your HTML :
<form action='destinationPage.php' method='post'>
<?php
echo $selectStart;
echo $selectEnd;
?>
<input type='submit' value='Submit' />
</form>

I am accessing MySQL table in PHP but it is not executing if condition and directly display the table

The code below is accessing the database table directly, but I want it to display the table content on giving conditions in drop down menu like when I select islamabad in one drop down menu and lahore in other as given in code and press search button, then it display the table flights, but it is displaying it directly
<p class="h2">Quick Search</p>
<div class="sb2_opts">
<p>
</p>
<form method="post" action="haseeb.php">
<p>Enter your source and destination.</p>
<p>
From:</p>
<select name="from">
<option value="Islamabad">Islamabad</option>
<option value="Lahore">Lahore</option>
<option value="murree">Murree</option>
<option value="Muzaffarabad">Muzaffarabad</option>
</select>
<p>
To:</p>
<select name="To">
<option value="Islamabad">Islamabad</option>
<option value="Lahore">Lahore</option>
<option value="murree">Murree</option>
<option value="Muzaffarabad">Muzaffarabad</option>
</select>
<input type="submit" value="search" />
</form>
</form> </table>
<?php
$from = isset($_POST['from'])?$_POST['from']:'';
$to = isset($_POST['to'])?$_POST['to']:'';
if( $from =='Islamabad'){
if($to == 'Lahore'){
$db_host = 'localhost';
$db_user = 'root';
$database = 'homedb';
$table = 'flights';
if (!mysql_connect($db_host, $db_user))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
die("Query to show fields from table failed");
}
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
}
}
mysqli_close($con);
?>
Fix the mysql_ and mysqli_.
Do not repeat the SQL Query to test $result. One is ok.
Fix the case of index - to or To.
Table displayed directly: Hope you are not refreshing the form with the pre-submitted values of your cities. Freshly open the page link, and confirm that $from, $to are really empty. If you are pressing F5 in browser, it may be wrong.

Mysql search all column together with WHERE and LIKE clause

I have a database where in the table there is four column id,title,address,phone.When searched with a selected table the query search the term in that particular row and result is fetched,but what I want to know is if no table is selected that is i.e-
<option value="">Select table</option>
I want the query to search the term in every table,I could do it with WHERE row1 LIKE %$keyword% OR row2 LIKE %$keyword%,but I want it to search this thing in one query as if it select all row data and search it with the term,like first it search the row 1 then row2 the row3 one after another on its own.Is there any way to do it
<html>
<head>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
$keyword= $_POST['keyword'];
$table= $_POST['table'];
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());
$data = mysql_query("SELECT * FROM information WHERE $table LIKE '%".$keyword."%'");
//And we display the results
while($result = mysql_fetch_array( $data ))
{
echo $result['id'];
echo $result['title'];
}
}
?>
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='POST'>
<input type='text' size='90' name='keyword'>
<select name='table'>
<option value="">Select table</option>
<option value="title">title</option>
<option value="address">address</option>
<option value="phone">phone</option>
</select>
<input type='submit' name='submit' value='submit' >
</form>
</body>
</html>
It looks like what you have should work, but you also need to concatenate the where clause. Try this one.
$data = mysql_query("SELECT * FROM information WHERE `{$table}` LIKE '%".$keyword."%'");

update existing row in mysql using php coding

in my db i have 20+ columns. i added 19 columns throught input form and stored in db succesfully. i fetch few rows from db in my main page. in my main page 1 more column is there. that is status column, it is a combo box type. if i click status column it should show 4 values. i want to select one of the values and then when i click save button it must go to stored in db with that same ID. how to do that? i tried but its not updated in mysql db...
mainpage combo box coding:
echo "\t<td><form action=statusdb.php method=post>
<select name=update><option value=empty></option><option value=Confirm>Confirm</option><option value=Processing>Processing</option><option value=Pending>Pending</option><option value=Cancelled>Cancelled</option></select>
<input name=\"update[".$a_row['slno']."]\"; type=submit id=id value=Save></form>
</td>\n";
status db coding:
if (isset($_GET['id']))
{
$id = mysql_real_escape_string($_GET['id']);
$sql = mysql_query("UPDATE guestdetails SET status = '" . $_POST['update'] ."'");
if(!$sql)
{
die("Error" .mysql_error());
}
}
help me how to do that?
In your IF should be $_POST, not $_GET
Also, need to add WHERE clause, like this:
if (isset($_POST['id']))
{
$id = mysql_real_escape_string($_POST['id']);
$update= mysql_real_escape_string($_POST['update']);
$sql = mysql_query("UPDATE guestdetails SET status = '$update' WHERE id='$id'");
if(!$sql)
{
die("Error" .mysql_error());
}
}
Also, you used name update twice, once in <select> once in <input>, take that one from <input> out, make it hidden field with name id and value of your slno row:
echo "\t<td>
<form action=statusdb.php method=post>
<select name=update>
<option value=empty></option>
<option value=Confirm>Confirm</option>
<option value=Processing>Processing</option>
<option value=Pending>Pending</option>
<option value=Cancelled>Cancelled</option>
</select>
<input name='id' type='hidden' value='".$a_row['slno']."';>
<input type='submit'>Save</button>
</form>
</td>\n";
Try like below:
<form action="statusdb.php" method="post">
<?php
while($a_row = mysql_fetch_array($sql)) {
$sl_no = $a_row['slno'];
echo '<select name="update['.$sl_no.']"><option value=empty></option><option value=Confirm>Confirm</option><option value=Processing>Processing</option><option value=Pending>Pending</option><option value=Cancelled>Cancelled</option></select> ';
echo '<input type="hidden" name="sl_no[]" value="'.$sl_no.'" />';
}
?>
<input name="update_rows" type="submit" value="Save">
</form>
<?php
if(isset($_POST['update_rows'])) {
$nums = $_POST['sl_no'];
$update = $_POST['update'];
foreach($nums as $sl) {
$sl_no = $sl;
$val = $update[$sl_no];
$sql_update = mysql_query("UPDATE guestdetails SET status = '$val' WHERE sl_no='$sl_no'");
}
}
?>
you are not submitting any id to status db.

Categories