PHP Insert table into database - php

I have a database table that I created from a .csv file. I used php to edit the content to match a new table on my database. I now need to INSERT that new table into the new database. I added form tags around the table and a submit button, and when I submit it I only get 1 or 2 random rows of the table. I am guessing that I might need a while or a for loop, but I cant figure out how to write it. Any assistance would be appreciated.
This is the code to post to the db
if(isset($_POST['submit'])){
$mr = $_POST['mr'];
$mrs = $_POST['mrs'];
$last_name = $_POST['last_name'];
$marital_status = $_POST['marital_status'];
$query = "INSERT INTO import_template(first_name, last_name, marital_status) VALUES('{$mr}', '{$last_name}', '{$marital_status}')";
$result = mysqli_query($connection, $query);
if(!$result){
die("QUERY FAILED " . mysqli_error($connection));
}
if(!$connection){
echo "We are NOT connected";
}
$query = "INSERT INTO import_template(first_name, last_name, marital_status) VALUES('{$mrs}', '{$last_name}', '{$marital_status}')";
$result = mysqli_query($connection, $query);
if(!$result){
die("QUERY FAILED " . mysqli_error($connection));
}
if(!$connection){
echo "We are NOT connected";
}
}
This is what the dynamic table looks like I only added the input to the first few to test, I haven't done the rest yet.
echo "<tr>";
echo "<td><input type='text' class='form-control' name='mr' value='$mr'></td>";
echo "<td><input type='text' class='form-control' name='last_name' value='$last_name'></td>";
echo "<td><input type='text' class='form-control' name='marital_status' value='$marital_status'></td>";
echo "<td>{$birthday}</td>";
echo "<td>Adult</td>";
echo "<td>{$anniversary}</td>";
echo "<td>{$mr_cell_phone}</td>";
echo "<td>{$home_phone}</td>";
echo "<td>{$mr_email}</td>";
echo "<td>{$street}</td>";
echo "<td>{$city}</td>";
echo "<td>{$state}</td>";
echo "<td>{$zip}</td>";
echo "<td></td>";
echo "</tr>";
echo "<tr>";
echo "<td><input type='text' class='form-control' name='mrs' value='$mrs'></td>";
echo "<td><input type='text' class='form-control' name='last_name' value='$last_name'></td>";
echo "<td><input type='text' class='form-control' name='marital_status' value='$marital_status'></td>";
echo "<td></td>";
echo "<td>Adult</td>";
echo "<td>{$anniversary}</td>";
echo "<td>{$mrs_cell_phone}</td>";
echo "<td></td>";
echo "<td>{$mrs_email}</td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "</tr>";

You can do like this. I have taken three fields such as task,agent and note.You can add as per your table rows:
main.php
<form id="first" action="insert_values.php" method="post">
<table>
<tr>
<td>Task</td>
<td>
<input type="text" name="task">
</td>
</tr>
<tr>
<td>Agent</td>
<td>
<input type="text" name="agent">
</td>
</tr>
<tr>
<td>Note</td>
<td><input type="text" name="Note"></td>
</tr>
</table>
<input type="submit" value="save" name="save">
</form>
insert_values.php
<?php
error_reporting( error_reporting() & ~E_NOTICE );
session_start();
include 'connect.php';
$task=$_POST['task'];
$agent=$_POST['agent'];
$Note=$_POST['Note'];
$result=mysql_query("insert into table_name(task,agent,t_note) values('$task','$agent','$Note')");
if($result)
{
$msg="Insert Successfully!!";
echo "<script>alert('Insert Successfully');document.location='main.php'</script>";
?>
<?php
}
else
{
$errmsg=" Insert not success!!";
echo "<script>alert('Insert Not Successfully');document.location='main.php'</script>";
}
?>

Related

Textareas causing problems to redirect to the next page

I have a database that is linked with the php. When I'm writing the first code, my web page gets redirected to the page 'mypage1.php'. But when I'm writing the second code, which is almost(exactly) the same, it doesn't get redirected to the mypage1.php. In the table inside my database, I have Orderid's that I'm using to relate to the php code. Could you tell me why doesn't the second code redirects to the same page? (It stays on the same page). The web page gets redirected to the mypage1.php when I am removing the text areas code from the 2nd code, particularly when I'm removing this code:
<input type='textarea' name='staffname'>
</td><td>
<input type='textarea' name='Time'>
</td><td>
<input type='textarea' name='custdetail'>
</td><td>
<input type='textarea' name='orderId'>
CODE 1 (PHP CODE):
echo "";
echo "<tr>";
echo "<td>idSpatula</td><td>Qauntity</td><td>Staff Name</td><td>Staff Id</td><td>Customer Details</d><td>Order Id</td>";
echo "</tr>";
echo "<tr>";
echo "<form >";
echo "<td>" ."Spatula Id=3"."</td><td>
<input type='textarea' name='3'>
</td><td>
<input type='textarea' name='staffname'>
</td><td>
<input type='textarea' name='Time'>
</td><td>
<input type='textarea' name='custdetail'>
</td><td>
<input type='textarea' name='orderId'>
<input type='submit' id='submit' value='submit'>
</td>
</form>";
echo "</tr>";
echo "</table>";
echo "</br>";
if(isset($_GET['3'])){
// means submit clicked!
$queryy3=0;
$queryy3 = $_GET['3'];
header('Location: mypage1.php');
$sqll3 = "UPDATE Spatula SET QuantityInStock=QuantityInStock-$queryy3 WHERE idSpatula=3 and QuantityInStock>0";
$sqlorder3 = "insert into `Order`(idOrder, RequestedTime, ResponsibleStaffMember, CustomerDetails) values (3,'1987-04-20 11:34:09','Shubhams','tell')";
$result1003 = mysqli_query($con, "SELECT QuantityInStock FROM Spatula where idSpatula=3");
mysqli_query($con, $sqlorder2);
if (mysqli_query($con, $sqll3) & $col1003['QuantityInStock']!=0 ) {
//$sql = "UPDATE Spatula SET QuantityInStock=QuantityInStock-$query WHERE idSpatula= and QuantityInStock>0";
echo "Updated";
}
while($col1003 = mysqli_fetch_array($result1003)) {
echo $col1003['QuantityInStock'];
if ($col1003['QuantityInStock']<=0){
header('Location: mypage2.php');
}
}
}
CODE 2:
echo "<table border='1'>";
echo "<tr>";
echo "<td>idSpatula</td><td>Qauntity</td><td>Staff Name</td><td>Staff Id</td><td>Customer Details</d><td>Order Id</td>";
echo "</tr>";
echo "<tr>";
echo "<form >";
echo "<td>" ."Spatula Id=4"."</td><td>
<input type='textarea' name='4'>
</td><td>
<input type='textarea' name='staffname'>
</td><td>
<input type='textarea' name='Time'>
</td><td>
<input type='textarea' name='custdetail'>
</td><td>
<input type='textarea' name='orderId'>
</td>
<input type='submit' id='submit' value='submit'>
</form>";
echo "</tr>";
echo "</table>";
if(isset($_GET['4'])){
// means submit clicked!
$queryy4=0;
$queryy4 = $_GET['4'];
header('Location: mypage1.php');
$sqll4 = "UPDATE Spatula SET QuantityInStock=QuantityInStock-$queryy4 WHERE idSpatula=4 and QuantityInStock>0";
$sqlorder4 = "insert into `Order`(idOrder, RequestedTime, ResponsibleStaffMember, CustomerDetails) values (5,'1987-04-20 11:34:09','Shusss','tss')";
$result1004 = mysqli_query($con, "SELECT QuantityInStock FROM Spatula where idSpatula=4");
mysqli_query($con, $sqlorder4);
if (mysqli_query($con, $sqll4) & $col1004['QuantityInStock']!=0 ) {
echo "Updated";
}
while($col1004 = mysqli_fetch_array($result1004)) {
echo $col1004['QuantityInStock'];
if ($col1004['QuantityInStock']<=0){
header('Location: mypage2.php');
}
}
}

insert multiple rows of data by single submit button using php

How do I retrieve data from a SQL table, modify the data and store it in another database table with multiple rows & columns and with single submit button I want insert every rows at a time I don't know how to get that hidden value and work properly with that
<?php
include"connect_database.php";
if(isset($_POST['submit'])) {
$amt = $_POST['total'];
if($amt > 0) {
$qry = "INSERT INTO attendance(rollno, name, year, attendance, reason) VALUES "; // Split the mysql_query
for($i=1; $i<=$amt; $i++) {
$qry .= "('".$_POST["rollno$i"]."', '".$_POST["name$i"]."', '".$_POST["year$i"]."', '".$_POST["attendance$i"]."', '".$_POST["reason$i"]."' ),"; // loop the mysql_query values to avoid more server loding time
}
$qry = substr($qry, 0, strlen($qry)-2);
$insert = mysqli_query($dbcon, $qry); // Execute the mysql_query
}
// Redirect for each cases
if($insert) {
$msg = '<script type="text/javascript">alert("added");</script>';
}
else {
$msg = '<script type="text/javascript">alert("Server Error, Kindly Try Again");</script>';
}
};
if (isset($_POST['select']))
{
$sql = "SELECT * FROM data WHERE year='" . $_POST['yr'] . "'";
$myData = mysqli_query($dbcon, $sql);
$num = mysqli_num_rows($myData);
echo "<table border=1>
<tr>
<th>Rollno</th>
<th>Name</th>
<th>Year</th>
<th>Attendance</th>
<th>reason</th>
</tr>";
for ($i=0; $i <$num; $i++)
{
$record = mysqli_fetch_array($myData);
echo "<form action=smanage.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=text name=rollno$i value=" . $record['rollno'] . " </td>";
echo "<td>" . "<input type=text name=name$i value=" . $record['name'] . " </td>";
echo "<td>" . "<input type=text name=year$i value=" . $record['year'] . " </td>";
echo "<td> "."<select name=attendance$i >
<option value=Present >present</option>
<option value=Absent >Absent</option>
</select>"."</td>";
echo "<td>". "<textarea cols=15 rows=2 name=reason$i placeholder=Enter reason ...></textarea>" . "</td>" ;
echo "<td>" . "<input type=hidden name=total value=" . $i-1 . "</td>";
echo "</tr>";
}
echo"</table>";
echo "<input type=submit name=submit value=save class=Button3>";
echo "</form>";
};
mysqli_close($dbcon);
?>
you are opening multiple forms, for each row in your table on.
This causes your html to be invalid, just start the form before displaying the table.
You could use this html
<table>
<?php
for ($i = 0; $i < $num; $i++) {
$record = mysqli_fetch_array($myData);
?>
<tr>
<td><input type="text" name="rollno[<?= $record['rollno'] ?>]" value="<?= $record['rollno'] ?>" </td>
<td><input type="text" name="name[<?= $record['rollno'] ?>]" value="<?= $record['name']?>" </td>
<td><input type="text" name="year[<?= $record['rollno'] ?>]" value="<?= $record['year'] ?>" </td>
<td><select name="attendance[<?= $record['rollno'] ?>]" >
<option value="Present" >present</option>
<option value="Absent" >Absent</option>
</select></td>
<td><textarea cols="15" rows="2" name="reason[<?= $record['rollno'] ?>]" placeholder="Enter reason ..."></textarea></td>
</tr>
<?php
}
?>
</table>
with this your values will every row will be put into the $_POST-Array, you can access the values via the indexes (I am guessing rollno represents the ID of the dataset).
When you really only want to insert all the values into a table, you can leave the index out. Meaning you could write
<td><input type="text" name="rollno[]" value="<?= $record['rollno'] ?>" </td>
Instead of
<td><input type="text" name="rollno[<?= $record['rollno'] ?>]" value="<?= $record['rollno'] ?>" </td>
You don't need the hidden field, you can just count the items in the array.
$total = count($_POST['rollno']);
<?php
include"connect_database.php";
if(isset($_POST['submit'])) {
$amt = $_POST['total'];
$rollnos= $_POST['rollno'];
if($amt > 0) {
$qry = "INSERT INTO attendance(rollno, name, year, attendance, reason) VALUES "; // Split the mysql_query
$i=0;
foreach($rollnos as $rollno) {
$qry .= "('".$rollno."', '".$_POST["name"][$i]."', '".$_POST["year"][$i]."', '".$_POST["attendance"][$i]."', '".$_POST["reason"][$i]."' ),"; // loop the mysql_query values to avoid more server loding time
$i=$i+1;
}
$qry = substr($qry, 0, strlen($qry)-2);
$insert = mysqli_query($dbcon, $qry); // Execute the mysql_query
}
// Redirect for each cases
if($insert) {
$msg = '<script type="text/javascript">alert("added");</script>';
}
else {
$msg = '<script type="text/javascript">alert("Server Error, Kindly Try Again");</script>';
}
};
if (isset($_POST['select']))
{
$sql = "SELECT * FROM data WHERE year='" . $_POST['yr'] . "'";
$myData = mysqli_query($dbcon, $sql);
$num = mysqli_num_rows($myData);
echo "<table border=1>
<tr>
<th>Rollno</th>
<th>Name</th>
<th>Year</th>
<th>Attendance</th>
<th>reason</th>
</tr>";
for ($i=0; $i <$num; $i++)
{
$record = mysqli_fetch_array($myData);
echo "<form action=smanage.php method=post>";
echo "<tr>";
echo "<td>" . "<input type='text' name='rollno[]' value='" . $record['rollno'] . "'> </td>";
echo "<td>" . "<input type='text' name='name[]' value='" . $record['name'] . "'> </td>";
echo "<td>" . "<input type='text' name='year[]' value='" . $record['year'] . "'> </td>";
echo "<td> "."<select name='attendance[]' >
<option value='Present' >present</option>
<option value='Absent' >Absent</option>
</select>"."</td>";
echo "<td>". "<textarea cols='15' rows='2' name='reason[]' placeholder='Enter reason ...'></textarea>" . "</td>" ;
echo "<td></td>";
echo "</tr>";
}
echo "<input type='hidden' name='total' value='" . $i-1 . "'>";
echo"</table>";
echo "<input type='submit' name='submit' value='save' class='Button3'>";
echo "</form>";
};
mysqli_close($dbcon);
?>

not understanding mysqli_fetch_array

So i guess i don't understand this i am a very newbie to coding in general. I have searched and can't find a good enogh explanation to get it to work in my situatation. I need to fill a table from a fetch command then update each result with an input of information into a new column. Here is the code i have:
This fills the table:
echo "<table border='1'>
<tr>
<th>Envelope</th>
<th>Budget</th>
<th>Amount</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['envelopename'] . "</td>";
echo "<td>" . $row['envelopebudget'] . "</td>";
?><td><input type="text" name="budgetamount"></td><?php;
echo "</tr>";
}
echo "</table>";
?>
<input type="submit">
And this writes the input for budgetname into the column budgetname:
$paycheckname = mysqli_real_escape_string($con, $_POST['paycheckname']);
$budgetamount = mysqli_real_escape_string($con, $_POST['budgetamount']);
$envelopename = mysqli_real_escape_string($con, $_POST['envelopename']);
}
$sql="UPDATE envelopes SET $paycheckname='$budgetamount' WHERE envelopename ='$envelopename'";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
Right now it just doesn't write anything. I have used echo var_dump($envelopename)."<br>"; and echo var_dump($budgetamount)."<br>"; to try and see what it is doing but $envelopename is always blank. Thanks for any help you can provide.
only <input> , <textarea>, <select> and <button> are submitted to the server in a form
If you want to re-submit some static values create some hidden inputs
while($row = mysqli_fetch_array($result)) {
echo "<input type='hidden' value='$row[envelopename]' name='envelopename'/>";
echo "<input type='hidden' value='$row[envelopebudget]' name='envelopebudget'/>";
echo "<tr>";
echo "<td>" . $row['envelopename'] . "</td>";
echo "<td>" . $row['envelopebudget'] . "</td>";
?><td><input type="text" name="budgetamount"></td><?php;
echo "</tr>";
}
echo "</table>";
But your next issue will be that you're creating the elements in a loop so you'll have multiple inputs with the same name
<tr>
<input type='hidden' value='envName1' name='envelopename'/>
<input type='hidden' value='envBudget1' name='envelopename'/>
<td>envName1</td>
<td>envBudget1</td>
<td><input type="text" name="budgetamount"></td>
</tr>
<tr>
<input type='hidden' value='envName2' name='envelopename'/>
<input type='hidden' value='envBudget2' name='envelopename'/>
<td>envName2</td>
<td>envBudget2</td>
<td><input type="text" name="budgetamount"></td>
</tr>
so you must submit as an array
while($row = mysqli_fetch_array($result)) {
echo "<input type='hidden' value='$row[envelopename]' name='envelopename[]'/>";
echo "<input type='hidden' value='$row[envelopebudget]' name='envelopebudget[]'/>";
echo "<tr>";
echo "<td>" . $row['envelopename'] . "</td>";
echo "<td>" . $row['envelopebudget'] . "</td>";
?><td><input type="text" name="budgetamount[]"></td><?php;
echo "</tr>";
}
echo "</table>";
and at the server end process as an array
foreach ($_POST['budgetamount'] as $budgetamount){
echo $budgetamount. '<br>';
}
suppose the form rendered looks like this:
<tr>
<input type='hidden' value='envName1' name='envelopename[]'/>
<input type='hidden' value='envBudget1' name='envelopebudget[]'/>
<td>envName1</td>
<td>envBudget1</td>
<td><input type="text" name="budgetamount[]"></td>
</tr>
<tr>
<input type='hidden' value='envName2' name='envelopename[]'/>
<input type='hidden' value='envBudget2' name='envelopebudget[]'/>
<td>envName2</td>
<td>envBudget2</td>
<td><input type="text" name="budgetamount[]"></td>
</tr>
<tr>
<input type='hidden' value='envName3' name='envelopename[]'/>
<input type='hidden' value='envBudget3' name='envelopebudget[]'/>
<td>envName3</td>
<td>envBudget3</td>
<td><input type="text" name="budgetamount[]"></td>
</tr>
when the user hits submit the $_POST that arrives at newpaycheck.php will look like:
$_POST
['envelopename']{
[0]=>'envName1',
[1]=>'envName2',
[2]=>'envName3'
},['envelopebudget']{
[0]=>'envBudget1',
[1]=>'envBudget2',
[2]=>'envBudget3'
},['budgetamount']{
[0]=>'someValueEnteredByUser',
[1]=>'anotherValueEnteredByUser',
[2]=>'yetAnotherValueEnteredByUser'
}
so you can do something like this:
foreach ($_POST['envelopename'] as $envelopename){
$arrayIndex = array_search($envelopename,$_POST['envelopename']);
$envelopebudget = $_POST['envelopebudget'][$arrayIndex];
$budgetamount= $_POST['budgetamount'][$arrayIndex];
$paycheckname = mysqli_real_escape_string($con, $envelopebudget);
$budgetamount = mysqli_real_escape_string($con,$budgetamount);
$envelopename = mysqli_real_escape_string($con,$envelopename);
$sql="UPDATE envelopes SET $paycheckname='$budgetamount' WHERE envelopename ='$envelopename'";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
}

Create dropdown list from another table into my update/delete/add table

Im a newbie and working on a project for school
I have a website that lists foods.
I have an update table that allows me to change and add data.
For the food group field I have it cross reference another table called food_group which has the food_group(name) and an id.
When you view the food data you can see the name that it pulls instead of the ID. On the update page I would like a drop down to be in the place of the ID. So you can see the "friendly" name instead of the ID number, but it has to store the ID not the friendly name in the food table.
Website can be found at http://web.nmsu.edu/~jrortiz/ICT458/FINAL/
The code I have is:
<html>
<head>
</head>
<body>
<?php
$con = mysqli_connect("localhost","user","pw","db");
if (!$con){
die("Can not connect: " . mysql_error());
}
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE food SET food_group='$_POST[Food_group]', food='$_POST[Food]', ph='$_POST[PH]' WHERE food='$_POST[hidden]'";
mysql_query($UpdateQuery, $con);
};
if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM food WHERE Food='$_POST[hidden]'";
mysql_query($DeleteQuery, $con);
};
if(isset($_POST['add'])){
$AddQuery = "INSERT INTO food (Food_group, Food, PH) VALUES ('$_POST[addGroup]','$_POST[addFood]','$_POST[addPH]')";
mysql_query($AddQuery, $con);
};
$sql = "SELECT * FROM food";
$myData = mysqli_query($con,$sql);
echo "<table border=1>
<tr>
<th>Food Group</th>
<th>Food</th>
<th>PH</th>
<th>Update/Add</th>
<th>Delete</th>
</tr>";
while($record = mysqli_fetch_array($myData)){
echo "<form action=updateFood.php method=post>";
echo "<tr>";
echo "<td><input type='text' name='Food_group' value='$record[food_group]'/></td>";
echo "<td><input type='text' name='Food' value='$record[food]'/></td>";
echo "<td><input type='text' name='PH' value='$record[ph]'/></td>";
echo "<td><input type='submit' name='update' value='update'/></td>";
echo "<td><input type='submit' name='delete' value='delete'/></td>";
echo "<td><input type='hidden' name='hidden' value='$record[food]'/></td>";
echo "</tr>";
echo "</form>";
}
echo "<form action=updateFood.php method=post>";
echo "<tr>";
echo "<td><input type='text' name='addGroup'></td>";
echo "<td><input type='text' name='addFood'></td>";
echo "<td><input type='text' name='addPH'></td>";
echo "<td><input type='submit' name='add' value='add'/></td>";
echo "</tr>";
echo "</form>";
echo "</table>";
mysql_close($con);
?>
</body>
</html>
____________ Update 12/2/13 10:30pm ___________________
Ok so if I create a new php page like the following it will work. However, I have no idea how to combine it into the original above... Can anyone help?
<html>
<head>
</head>
<body>
<?php
// Connect to the database server
$con = mysql_connect("localhost","user","pw");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("db",$con);
$sql2="SELECT id, food_group FROM food_group";
$result = mysql_query($sql2,$con) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$type=$row["food_group"];
$options.= '<option value="'.$row['id'].'">'.$row['food_group'].'</option>';
};?>
<SELECT NAME=Food_group>
<OPTION VALUE=0>Choose</OPTION>
<?php echo $options; ?>
</SELECT>
</body>
</html>
Thank you for all your help!
Jason
Your script is nice but I just want to point the following:
There's no need to concatenate this
"<td>" . "<input type=text name=Food_group value=" . $record['food_group'] . " </td>";
you can type it like this:
echo "<td><input type=text name=Food_group value='$record[food_group]'</td>";
also you missed to close your input tag
echo "<td><input type=text name=Food_group value='$record[food_group]' /></td>";
and another is you need to quote your attribute values , see below
echo "<td><input type='text' name='Food_group' value='$record[food_group]'</td>";
Last thing is that you're open to SQL injection, so you should start learning mysqli and prepared statement

Link (on a while loop on a search query) inside a search form

echo "<center><form name = 'searching' method='POST' action='cpanel.php?manage=".$useraccounts."&rcad=".$viewcustomer."'><table border = 1>";
echo "<select name = 'filter'>";
echo "<option value ='username'>Username</option>";
echo "<option value ='username'>Username</option>";
echo "</select>";
echo " <input name='search' type='text' > ";
echo "<input type='submit' name='submit' value='Search'> ";
echo "<input type='submit' name='back' value='Back'><br><br>";
echo "<tr>";
echo "<td>User ID</td>";
echo "<td>Username</td>";
echo "<td>Last Name</td>";
echo "<td>First Name</td>";
echo "<td>Middle Initial</td>";
echo "<td>Address</td>";
echo "<td>Contact Number</td>";
echo "<td>Birthday</td>";
echo "<td>Date Registered</td>";
echo "<td> </td>";
echo "</tr>";
$submit = $_POST['submit'];
if(isset($submit)) {
$search = $_POST['search'];
$filter = $_POST['filter'];
include "dbconnect.php";
if ($search != NULL) {
$searchquery = mysql_query("SELECT * FROM register WHERE $filter LIKE '%$search%'");
while($fetchres = mysql_fetch_array($searchquery)) { //show search results
$userid = $fetchres['userid'];
$username = $fetchres['username'];
$lname = $fetchres['lname'];
$fname = $fetchres['fname'];
$mi = $fetchres['mi'];
$address = $fetchres['address'];
$contact = $fetchres['contact'];
$month = $fetchres['month'];
$day = $fetchres['day'];
$year = $fetchres['year'];
$dateregistered = $fetchres['date'];
$sendmessage = "<a href = 'cpanel.php?manage=".$useraccounts."&rcad=".$viewcustomer."&user=".$username."'>Send message</a>";
echo "<tr>";
echo "<td>$userid</td>";
echo "<td>$username</td>";
echo "<td>$lname</td>";
echo "<td>$fname</td>";
echo "<td>$mi</td>";
echo "<td>$address</td>";
echo "<td>$contact</td>";
echo "<td>$month $day, $year</td>";
echo "<td>$dateregistered</td>";
echo "<td>$sendmessage</td>";
echo "</tr>";
echo "$table";
if (isset($sendmessage)) {
$getuser = $_GET['user'];
if ($getuser == $username) {
//start send message
$touser = $username;
$fromuser = $adminsess;
$subject = $_POST['subject'];
$message = $_POST['message'];
$submit = $_POST['submit'];
$date = date("Y-m-d");
$rand = rand(98765432,23456789);
$table = '<center><script type="text/javascript" src="/js/sendmessage.js"></script>
<form action="cpanel.php?manage='.$useraccounts.'&rcad='.$viewcustomer.'&user='.$username.'&send='.$one.'" method="post" name="sendpm" onsubmit="return valid()">
<table>
<tr>
<td>
To:
</td>
<td>
'.$touser.'
</td>
</tr>
<tr>
<td>
Subject:
</td>
<td>
<input type="text" name="subject" id="subj1" />
</td>
</tr>
<tr>
<td>
Message:
</td>
<td>
<textarea name="message" cols="60" rows="10" id="mes1"></textarea>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name = "submit" value="Submit" />
</td>
</tr>
</table>
</form></center>';
if (isset($submit))
{
$send = $_GET['send'];
if ($send == $one) {
include "maildbconnect.php";
$query = mysql_query("INSERT INTO mailtbl_admin VALUES ('', '$touser', '$fromuser', '$subject',
'$message', '0', '0', '1', '$date', '$rand')");
echo "Message successfully sent.";
}
}
}//end send message
}//end $getuser
}
}
The form for sending a message ($table) doesn't appear after clicking the link ($sendmessage) on a certain search result. After clicking the link, i'm prompted to an empty table (table not inside the while loop) but the $_GET function is working. Can anyone tell me how to fix this? thanks a lot
$submit wont be set when following the link. So any code after:
if(isset($submit)) {
Will not fire.
You've got a lot wrong with this form. First off, you forgot a <tr> and <td> after your <table> tag.
echo "<center><form name = 'searching' method='POST' action='cpanel.php?manage=".$useraccounts."&rcad=".$viewcustomer."'><table border = 1><tr><td colspan='10'>";
echo "<select name = 'filter'>";
echo "<option value ='username'>Username</option>";
echo "<option value ='username'>Username</option>";
echo "</select>";
echo " <input name='search' type='text' > ";
echo "<input type='submit' name='submit' value='Search'> ";
echo "<input type='submit' name='back' value='Back'><br><br>";
echo "</td></tr>";
Alternatively you could move the opening <table> tag below the <select> tag.
After you fix that, try moving the logic where you insert the values into mailtbl_admin outside of the loop. I'm guessing you're only going to want to send the message to one user, so it only makes sense to move it out of the loop.

Categories