insert in sql with mysqli and cookie in php - php

I have two files: Studio.php and Principale.php
In the first one i want insert in my database some values and add them also in a table of Principale.php
With the first row of the table in Principale.php and the first add in the table of my database it works, after no.
I thought to use a for loop to create more rows in the table of Principale.php
It shows me also this error : Notice: Undefined index: titolo1 in C:\xampp\htdocs\Progetto\Principale.php on line 47
I think $res2 return a value different than TRUE but i don't know why.
Code of Principale.php :
$n=$_COOKIE['idstudio'];
for ($i=1;$i<$n;$i++) {
echo "<table style='border: 1px solid black; margin-left:auto; margin-
right:auto;'><caption> <p>Esperienze di Studio</p>
</caption> <thead style='color:green;'> <tr>";
echo "<th style='border: 1px solid black;'> Titolo </th><th
style='border: 1px solid black;'> Anno </th><th style='border: 1px solid
black;'> Luogo </th> ";
echo "</tr> </thead> <tbody style='color:blue'> <tr>";
echo "<td style='border: 1px solid black;'>".$_COOKIE["titolo$i"]."
</td>";
echo "<td style='border: 1px solid black;'>".$_COOKIE["anno$i"]."</td>";
echo "<td style='border: 1px solid black;'>".$_COOKIE["luogo$i"]."
</td>";
echo "</tr></tbody</table><br><br>";
}
echo "</div>";
Code of Studio.php :
$luogo = $_POST["luogo"];
$anno = $_POST["anno"];
$titolo = $_POST["titolo"];
$sql6="SELECT education_experience_id FROM education_experience ORDER BY
education_experience_id DESC LIMIT 1;";
$result2=mysqli_query($connessione,$sql6);
$row2=mysqli_fetch_array($result2,MYSQLI_NUM);
$last_id2=$row2[0];
$id2 = $last_id2+1;
$eeid=$_COOKIE['id'];
$sql9="INSERT into EDUCATION_EXPERIENCE
(education_experience_id,title,year,place,user_id) VALUES
('$id2','$titolo','$anno','$luogo','$eeid')";
$res2=mysqli_query($connessione,$sql9);
echo "<br>";
mysqli_error($connessione);
if($res2 ==TRUE) {
setcookie("luogo$id2", $luogo, strtotime("+1 month"));
setcookie("anno$id2", $anno, strtotime("+1 month"));
setcookie("titolo$id2", $titolo, strtotime("+1 month"));
setcookie("idstudio",$id2,strtotime("+1 month"));
header("Location: Principale.php");
}
else
{
echo "Impossibile aggiungere Esperienza<br>";
echo "<a href='Principale.php'>Clicca Qui</a> per tornare alla pagina
principale.";
}

Related

How do i put a <td> in my <table> with php?

I’m working on a php snippet and i made a table. I tried putting a td tag inside but when i do this, a lot disappears. This is a piece of my code:
//Use the functions of the client, the params of the function are in
//the associative array
$params = array('customerid' => '1532');
$response = $soapclient->ca_customer_products($params);
echo '<table><tbody><tr><th>Product</th><th>Naam</th> <th>Prijs</th><th>Qte</th></tr>';
echo '<table style="border-style: solid; border-width:1px;">';
foreach($response->list->element as $product) {
if($product->stock > 0) {
echo '<tr>';
echo '<td style="display: flex; border: 1px solid black;">';
//echo '<td>';
echo '<img src="' . $product->url . '" class="php_image" style="width: 15%; height: 15%;"/>';
//echo '<img style="width: 15%;">';
//echo '</td>';
print_r($product->description);
echo "<p style='color:green;'>".$product->price1."</p>";
echo "<p style='color:red; text-decoration: line-through'>".$product->price2."</p>";
print_r($product->price1);
print_r($product->price2);
print_r($product->stock);
echo '</tr>';
}
}
echo '</tbody></table>';
The code behind the // is where i tried to put the td tag but when i put it there, the images that normally appear go blank and when i inspect my code there is a lot of other code that also disappears. What am i doing wrong here?
Thank you for your help!
First I can see a problem with these lines :
echo '<table><tbody><tr><th>Product</th><th>Naam</th> <th>Prijs</th><th>Qte</th></tr>';
echo '<table style="border-style: solid; border-width:1px;">';
Because you just close your first TABLE at the end, but not the other inside :
echo '</tbody></table>';
There are some inconsistency of your <td> to <th>. You can have a look below
$params = array('customerid' => '1532');
$response = $soapclient->ca_customer_products($params);
echo '<table style="border-style: solid; border-width:1px;">
<thead>
<tr>
<th>Product</th>
<th>Naam</th>
<th>Prijs</th>
<th>Qte</th>
</tr>
</thead><tbody>';
foreach($response->list->element as $product) {
if($product->stock > 0) {
echo "<tr>
<td style='display: flex; border: 1px solid black;'>
<img src='$product->url' class='php_image' style='width: 15%; height: 15%;'/>
</td>
<td>Your product name</td>
<td>$product->description</td>
<td>
<p style='color: green;'>$product->price1</p>
<p style='color: red;'>$product->price</p>
</td>
<td>$product->stock</td>
</tr>";
}
}
echo '</tbody></table>';
?>
There's a lot problems with the code. From what i can see in your code, you have a table inside a tbody and at the end closed only one table.
Secondly you're also trying to put a td inside another td which is not the right thing to do. Check out mozilla developer website for more information on using HTML tables.
//Use the functions of the client, the params of the function are in
//the associative array
$params = array('customerid' => '1532');
$response = $soapclient->ca_customer_products($params);
echo '<table style="border-style: solid; border-width:1px;"><tbody><tr><th>Product</th><th>Naam</th> <th>Prijs</th><th>Qte</th></tr>';
foreach($response->list->element as $product) {
if($product->stock > 0) {
echo '<tr>';
echo '<td style="display: flex; border: 1px solid black;">';
//echo '<td>';
echo '<img src="' . $product->url . '" class="php_image" style="width: 15%; height: 15%;"/>';
//echo '<img style="width: 15%;">';
//echo '</td>';
print_r($product->description);
echo "<p style='color:green;'>".$product->price1."</p>";
echo "<p style='color:red; text-decoration: line-through'>".$product->price2."</p>";
print_r($product->price1);
print_r($product->price2);
print_r($product->stock);
echo '</td></tr>';
}
}
echo '</tbody></table>';
You are not closing your tags correctly. Also checkout the docs just as #christopher_bincom has mentioned.

Search and show duplicate data php mysql

I want to search and execute the data with the same orderid . Unfortunately it says
Fatal error: Cannot use object of type mysqli_result as array in
C:\wamp64\www\Workshop1\admin\ordersearch.php on line 114
if(isset($_POST['search']))
{
// id to search
$Orderid = $_POST['Orderid'];
// connect to mysql
$connect = mysqli_connect("localhost", "root", "","workshop1");
// mysql search query
$query = "SELECT * FROM orders WHERE Orderid = $Orderid ;";
$result = mysqli_query($connect, $query);
// if id exist
// show data in inputs
if(mysqli_num_rows($result) > 0)
{
while ($row = mysqli_fetch_array($result))
{?>
<td style='border-bottom: 1px solid #b3b3b3;' align='center'><?php echo $result["Orderid"];?></td>
<td style='border-bottom: 1px solid #b3b3b3;' align='center'><?php echo $result["productid"];?></td>
<td style='border-bottom: 1px solid #b3b3b3;' align='center'><?php echo $result["price"];?></td>
<td style='border-bottom: 1px solid #b3b3b3;' align='center'><?php echo $result["quantity"];?></td>
<td style='border-bottom: 1px solid #b3b3b3;' align='center'><?php echo $result["custid"];?></td>
<?php
}
}
// if the id not exist
// show a message and clear inputs
else {
echo "Undifined ID";
}
mysqli_free_result($result);
mysqli_close($connect);
}
you must write your code like this because if you don't it will recognize $Orderid as
a string.
$query = "SELECT * FROM orders WHERE Orderid = '" . $Orderid. "' limit 1 ";
and change this
<td style='border-bottom: 1px solid #b3b3b3;' align='center'>
<?php echo $row["Orderid"];?>
</td>
You are store data in loop using $row variable so that you need to use $row['orderId']; if you print data in loop for ex. print_r($row);

How to avoid code being inserted into database

How to avoid code being inserted into database(Eg: ) while still maintaining the css applied on the div's from a textbox. Please see the image below and look for column openletter you will see html code is being inserted into this column. There are div's along with css being inserted. I know it sounds silly but how to avoid these codes being inserted into db. I have attached my model,view and controller codes below.
My model code is (student.php):
public function updatestudent($open_id, $from, $to, $openletter, $featured, $title, $archieve, $latest, $sponsor, $image, $category)
{
$data = array('open_id'=>$open_id, 'from'=>$from, 'to'=>$to, 'openletter'=>$openletter, 'featured'=>$featured, 'title'=>$title, 'archieve'=>$archieve, 'latest'=>$latest, 'sponsor'=>$sponsor, 'image'=>$image, 'category'=>$category);
$this->db->where('open_id', $open_id);
return($this->db->update('country',$data));
}
My controller code is(home.php):
public function editstudent($open_id)
{
$query['data']=$this->student->showstudentCon($open_id);
if (isset($_POST['submit']))
{
$this->form_validation->set_rules('open_id', 'open_id', 'required');
$this->form_validation->set_rules('from', 'from', 'required');
$this->form_validation->set_rules('to', 'to', 'required');
$this->form_validation->set_rules('openletter', 'openletter', 'required');
$this->form_validation->set_rules('featured', 'featured', 'required');
$this->form_validation->set_rules('title', 'title', 'required');
$this->form_validation->set_rules('archieve', 'archieve', 'required');
$this->form_validation->set_rules('latest', 'latest', 'required');
$this->form_validation->set_rules('sponsor', 'sponsor', 'required');
$this->form_validation->set_rules('image', 'image', 'required');
$this->form_validation->set_rules('category', 'category', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('file/header');
$this->load->view('file/menu');
$this->load->view('form', $query);
$this->load->view('file/footer');
}
else {
$open_id=$_POST['open_id'];
$from=$_POST['from'];
$to=$_POST['to'];
$openletter=$_POST['openletter'];
$featured=$_POST['featured'];
$title=$_POST['title'];
$archieve=$_POST['archieve'];
$latest=$_POST['latest'];
$sponsor=$_POST['sponsor'];
$image=$_POST['image'];
$category=$_POST['category'];
$result=$this->student->updatestudent($open_id, $from, $to, $openletter, $featured, $title, $archieve, $latest, $sponsor, $image, $category);
if($result)
{
$this->load->view('file/header');
$this->load->view('file/menu');
echo "<div class='success'>";
echo "Successfully Updated";
echo "</div>";
$this->load->view('file/footer');
}
else {
$this->load->view('file/header');
$this->load->view('file/menu');
echo "<div class='error'>";
echo "Somthins Is Missing";
echo "</div>";
$this->load->view('file/footer');
}
}
}
else {
$this->load->view('file/header');
$this->load->view('file/menu');
$this->load->view('form', $query);
$this->load->view('file/footer');
}
}
My view Code is (demoview.php):
<script>
$(document).ready(function() {
$('#datatable').DataTable();
} );
</script>
<div class="content">
<h2>Welcome Back, <?php echo $name=$this->session->userdata('username'); ?>!</h2>
<h2>Open Letters</h2>
<div class="divider"></div>
<br/>
<?php
echo "<table style='border: 1px solid black' id='datatable' class='display' cellspacing='0' width='100%'>";
$head="<thead>
<tr style='border: 1px solid black'>
<th>From</th>
<th>To</th>
<th>Title</th>
<th>open_id</th>
<th>archieve</th>
<th>latest</th>
<th>sponsor</th>
<th>Image</th>
<th>category</th>
</tr>
</thead>";
$foot="<tfoot>
<tr style='border: 1px solid black'>
<th>From</th>
<th>To</th>
<th>Title</th>
<th>open_id</th>
<th>archieve</th>
<th>latest</th>
<th>sponsor</th>
<th>Image</th>
</tr>
</tfoot>";
echo $head;
echo $foot;
echo "<tbody>";
foreach($query as $row)
{
echo "<tr style='border: 1px solid black'>";
echo "<td style='border: 1px solid black'>";
echo $row->from;
echo "</td><td style='border: 1px solid black'>";
echo $row->to;
echo "</td><td style='border: 1px solid black'>";
echo $row->title;
echo "</td><td style='border: 1px solid black'>";
echo $row->open_id;
echo "</td><td style='border: 1px solid black'>";
echo $row->archieve;
echo "</td><td style='border: 1px solid black'>";
echo $row->latest;
echo "</td><td style='border: 1px solid black'>";
echo $row->sponsor;
echo "</td><td style='border: 1px solid black'>";
echo $row->image;
echo "</td><td style='border: 1px solid black'>";
echo $row->category;
echo "</td><td style='border: 1px solid black'>";
echo "<a href='".base_url('index.php/home/editstudent').'/'.$row->open_id."'>Edit </a><a href='".base_url('index.php/home/deletestudent').'/'.$row->open_id."'>Delete</a>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>
<h4><?php echo anchor('home/logout', 'Logout'); ?></h4>
</div><!--<div class="content">-->
First in your Model you don't need to return an update simply do
public function updatestudent($open_id, $from, $to, $openletter, $featured, $title, $archieve, $latest, $sponsor, $image, $category)
{
$data = array('open_id'=>$open_id, 'from'=>$from, 'to'=>$to, 'openletter'=>$openletter, 'featured'=>$featured, 'title'=>$title, 'archieve'=>$archieve, 'latest'=>$latest, 'sponsor'=>$sponsor, 'image'=>$image, 'category'=>$category);
$this->db->where('open_id', $open_id);
$this->db->update('country',$data);
}
Secondly it is engouh to just do $openletter = strip_tags($_POST['openletter']);
And the html will be gone. please see strip_tags
strip_tags — Strip HTML and PHP tags from a string
string strip_tags ( string $str [, string $allowable_tags ] )
SOURCE: Official Documentation
So, using strip_tags your controller will have:
$openletter=strip_tags($_POST['openletter']);
You can use strip_tags in your model file :-
public function updatestudent($open_id, $from, $to, $openletter, $featured, $title, $archieve, $latest, $sponsor, $image, $category)
{
$openletter = strip_tags($openletter);
$data = array('open_id'=>$open_id, 'from'=>$from, 'to'=>$to, 'openletter'=>$openletter, 'featured'=>$featured, 'title'=>$title, 'archieve'=>$archieve, 'latest'=>$latest, 'sponsor'=>$sponsor, 'image'=>$image, 'category'=>$category);
$this->db->where('open_id', $open_id);
return($this->db->update('country',$data));
}
It may help you.

sending checkbox data in table through mail

I am trying to send selected check-box data through mail in the tables structure but getting below error:
implode(): Invalid arguments passed
static function mcontent($chkbox){
$sql = "SELECT station,reach,language,rate FROM `radio_city` where chkid in (".implode(',', $chkbox).")";
return DB::select($sql);
}
Please help.
Thanks
Jyoti
View1: this is used while sending data through mail
<input type="hidden" name="checkbox" id="checkbox" >
<input type="hidden" name="hiddamount" id="hiddamount">
<input type="email" name="email" id="email">
<input type="submit" id="submit" class="button" style="box-shadow: 5px 5px 3px #888888;" value="Email Plan" onsubmit="return validateForm();checkbox();"/>
function checkbox(){
if (document.myForm.checkbox.checked){
document.getElementById("error").innerHTML = "";
return true;
}
else {
document.getElementById("error").innerHTML = "Select your Plans";
}
}
View2: this is used while selecting checkboxes
<td onchange="Process(this.options[this.selectedIndex].value)" class="chart_row" align="center" >
<input type="checkbox" name="checkbox[<?php echo $i; ?>]" id="checkbox" value="<?php echo $chkid; ?>" data-price="<?php echo $total; ?>" onChange="updateTotal(this);">
</td>
<td id="station" class="chart_row" align="center"><?= $result->station ?></td>
<td id="reach" class="chart_row" align="center"><?= $result->reach ?></td>
<td id="language" class="chart_row" align="center"><?= $result->language ?></td>
<td id="rate" class="chart_row" align="center">Rs <?= $result->rate ?>/-</td>
<td id="duration" class="chart_row" align="center"><?= $duration ?></td>
<td id="frequency" class="chart_row" align="center"><?= $frequency ?></td>
<td id="hours" class="chart_row" align="center"><?= $hours ?></td>
<?php $totals += ($result->rate) * $duration * $frequency * $hours * $days/10; ?></div>
</tr>
Controller:
function mail() {
$input = Input::get();
if (isset($_GET['checkbox'])){
$city = $_GET['city'];
$duration = $_GET['duration'];
$frequency = $_GET['frequency'];
$hours = $_GET['hours'];
$days = $_GET['days'];
$total = $_GET['hiddamount'];
$chkbox = $_GET['checkbox'];
$mailrslt = Radio::mcontent($chkbox);
$cityname = Radio::getall($input);
foreach($cityname as $cities){
//modify table ans add css here if you want.
$html_table ="<table><caption>City: ".$cities->city."</caption><thead style='color:A0A0A0'><th width='200' align='center' class='tabledata' style='background: #f9f9f9;color: #000000;;border-bottom: 1px solid #d6d6d6;border-right: 1px solid #d6d6d6;padding: 5px 10px;'>Station</th>
<th width='200' align='center' class='tabledata' style='background: #f9f9f9;color: #000000;;border-bottom: 1px solid #d6d6d6;border-right: 1px solid #d6d6d6;padding: 5px 10px;'>Reach</th>
<th width='200' align='center' class='tabledata' style='background: #f9f9f9;color: #000000;;border-bottom: 1px solid #d6d6d6;border-right: 1px solid #d6d6d6;padding: 5px 10px;'>Language</th>
<th width='200' align='center' class='tabledata' style='background: #f9f9f9;color: #000000;;border-bottom: 1px solid #d6d6d6;border-right: 1px solid #d6d6d6;padding: 5px 10px;'>Rate</th>
<th width='200' align='center' class='tabledata' style='background: #f9f9f9;color: #000000;;border-bottom: 1px solid #d6d6d6;border-right: 1px solid #d6d6d6;padding: 5px 10px;'>Jingle Lenght</th>
<th width='200' align='center' class='tabledata' style='background: #f9f9f9;color: #000000;;border-bottom: 1px solid #d6d6d6;border-right: 1px solid #d6d6d6;padding: 5px 10px;'>Frequency</th>
<th width='200' align='center' class='tabledata' style='background: #f9f9f9;color: #000000;;border-bottom: 1px solid #d6d6d6;border-right: 1px solid #d6d6d6;padding: 5px 10px;'>Hours/Day</th>
<th width='200' align='center' class='tabledata' style='background: #f9f9f9;color: #000000;;border-bottom: 1px solid #d6d6d6;border-right: 1px solid #d6d6d6;padding: 5px 10px;'>Days/Week</th></thead><tbody style='color: #000000;'>";}
//whole query result is here
foreach($mailrslt as $key=>$row) {
$html_table .= "<tr>";
//Iterate your data for table data.
foreach($row as $key2=>$row2){
$html_table .= "<td width='200' align='center' class='tabledata'>" . $row2 . "</td>";
}
//Database data ends here
$html_table .= "<td width='200' align='center' class='tabledata'>" . $duration . "</td>";
$html_table .= "<td width='200' align='center' class='tabledata'>" . $frequency . "</td>";
$html_table .= "<td width='200' align='center' class='tabledata'>" . $hours . "</td>";
$html_table .= "<td width='200' align='center' class='tabledata'>" . $days . "</td>";
} //table rows ends here for displaying data
$html_table .= "</tr></tbody></table>";
//contact details and total calculation done here
$html_table .= "<p>The total negotiable cost for your activity is <b>Rs ".$total."/-</b></p><div float='right'></p><u><b>For Best Rates</b></u></br> Call: Samir-+919686595443</br>Email: samir#themediaant.com</p></div>";
$to = $input['email'];//user email id from form.
$from = 'help#themediaant.com';//from email ID
$subject = 'Self Help Radio Planner';//subject
$totals = 0;
$message = $html_table;//assigning html_table variable to message.
$contact_enquiry = new ContactEnquiry;
$contact_enquiry->email = $from;
$contact_enquiry->message = $message;
$contact_enquiry->save();
//final mail function goes here
$mail = Mail::send('emails.media_option_assistance', ['msg' => $message], function ($msg) use ($from, $to, $subject) {
$msg->from($from, 'The Media Ant');
$msg->to($to)->subject($subject);//->cc('servicing#themediaant.com');
});
//after successful mail redirect user to same page.
return Redirect::to('/radio-plan/radioplan')->with('message','<b>Congratulations! You have succesfully sent the email');
}
}
Model:
static function mcontent($chkbox){
$sql = "SELECT station,reach,language,rate FROM `radio_city` where chkid in (".implode(',', $chkbox).")";
return DB::select($sql);
}
Your problem is here
$chkbox = $_GET['checkbox'];
$mailrslt = Radio::mcontent($chkbox);
implode requires the second parameter (or the first if only one is supplied) to be an array. You are providing mcontent with a string that you get from the input. So when you reach implode inside mcontent, you are trying to convert a string to a string.
So you must either create an array with one or more variables to be passed to your function, or remove the implode from the function.
I would try to help more but honestly I don't understand much from your code and it would be a guessing game.

Border around specific table row

I am trying to create a border around the top row ONLY! Currently, a border will be displayed only around the outside of the table. But I also want a border around the first row. Can someone help me do this? I want the row with 'Team, Correct Picks, and Points' to have a border around it.
<body>
<?=$leaguename?>
<center><table cellspacing="0" style="width:400px; border:1px solid gray">
<?
echo "<tr border=\"1\"><td> Team </td><td>Correct Picks</td><td>Points</td></tr>";
while($row = mysql_fetch_array($memberslist)) {
if ($row['User_ID'] == $id) {
echo "<tr style=\"border:1px solid gray\" bgcolor=\"gray\"><td>" . $row['User_ID'] . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
} else {
echo "<tr><td>" . $row['User_ID'] . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
}
}
?>
</table></center>
</body>
This is presentational, so mixing it into the PHP is a bad idea. Instead, use CSS:
tr:first-child td { border-top: 1px solid black; border-bottom: 1px solid black; }
tr:first-child td:first-child { border-left:1px solid black; }
tr:first-child td:last-child { border-right:1px solid black; }
IE7+8 support for the *-child selectors can be a bit buggy. If it's not working in those browsers, consider using JavaScript polyfills.
You may also want to put the headers inside <thead> and <th> elements to make the <tr> with data the first row in a <tbody> element.
e.g.
<table>
<thead>
<tr>
<th>Team</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
</tr>
</tbody>
</table>

Categories