I have this table on saprfc, and I import it into the sql server. But the person's name with (') does not enter the sql server. (Nanang Ma'ruf and Fat'khah Dwi)
ID | FEE | NAME | PERIOD
000711 | 204000 | YUDI MANDALA | 201807
000790 | 84000 | NANANG MA'RUF | 201807
001171 | 151500 | SARJANA | 201807
012314 | 89000 | FAT'KHAH DWI | 201807
my code:
foreach($tmp as $item)
{
$i++
$a[$i] = $item['id'];
$b[$i] = $item['FEE'];
$c[$i] = $item['NAME'];
$d[$i] = $item['PERIOD'];
echo '<tr>';
echo '<td>'. $item['ID'].'</td>';
echo '<td>'. $item['FEE'].'</td>';
echo '<td>'. $item['NAME'].'</td>';
echo '<td>'. $item['PERIOD'].'</td>';
echo '</tr>';
}
?>
</tbody>
</table>
</div>
</div>
<?php
if (isset($_POST["import"]))
{
if ($FI_HPs!=0)
{
for ($i=1;$i<=$FI_HPs;$i++)
{
$query = mssql_query("INSERT INTO SISDM (id, fee, name, period) VALUES ('$a[$i]','$b[$i]','$c[$i]','$d[$i]')");
}
}
}
You can use mysqli_real_escape_string().
PS: i'll use PDO instead of mssql_query(), 'cause it's removed in PHP7 so you can use PDO::quote() instead of previous escape function.
Related
I want to export my code to excel however the header is also dynamic. Here is my code, i need assistance in exporting my data. This code below shows a dynamic table i showed in user view and needs to be exported. The view is:
"title: course/section"
"header: student id | student name | program | term | dynamic date 1 | Remarks (remarks why u are absent) including date 1 | date 2 | Remarks (remarks why u are absent) including date 2 | and so on.."
"table data: 20122222 |pinky webb |computer science | 3 | Absent | With medical certificate | present | no remark | present | no remark | late | no remark | and do on... "
"table data 2: 20122333 |Mane Sharpay|computer science | 3 | Absent | With medical certificate | present | no remark | Late | no remark | late | no remark | and so on... "
and so on...
basically, it shows the student and its attendance per date horizontally with a dynamic header of dates. sorry if im noob hehe but ill give a thumbs up i promise for ur effort
<?php
$query = "SELECT course_id, sections_id from current_att_view inner join professor_tbl on current_att_view.professor_id = professor_tbl.professor_id where professor_live_account = '".$_SESSION['username']."' group by course_id, sections_id";
$result1 = mysqli_query($link, $query);
while ($col1 = mysqli_fetch_array($result1))
{
$reslt;
echo '<h3 class="course-sect">'.$col1[0].'/'.$col1[1].'</h3>';
$qry = "Call Get_Attendance_Course_Section('".$_SESSION['username']."','".$col1[0]."','".$col1[1]."')";
$reslt = mysqli_query($link, $qry);
echo '<table class="table table-bordered">';
echo '<tr>';
if (!$reslt) {
printf("Error: %s\n", mysqli_error($link));
exit();
}
else{
while ($fieldinfo = mysqli_fetch_field($reslt)) {
if($fieldinfo->name != "Course" && $fieldinfo->name != "Section" && $fieldinfo->name != "Course Name" && $fieldinfo->name != "Schedule")
{
echo '<th>';
echo $fieldinfo->name;
echo '</th>';
}
}
echo '</tr>';
while ($rows = mysqli_fetch_array($reslt))
{
for ($i=0; $i < (count($rows)/2); $i++) {
if($i != 3 && $i != 4 && $i != 5 && $i != 6){
echo '<td>';
echo $rows[$i];
echo '</td>';
}
}
echo '</tr>';
}
echo '</table>';
mysqli_next_result($link);
}
}
// $reslt =mysqli_query($link, $qry);
?>
<input type=hidden name=date value='<?php echo date("F d, Y",strtotime($date));?>'>
<input type="hidden" name="outy" value="<?php echo $sql; ?>">
<input type="submit" name="export" value="Export" class="submit" />
I've got two tables in mySQL.
Table: Player
-----------------------------------------------------------
| name | T1 | T2 | T3 | T4 | T5 | id | weeknumber |
|--------|----|-----|-----|-----|-----|-----|-------------|
| | | | | | | | |
| | | | | | | | |
-----------------------------------------------------------
Table: Teams
-------------------------------------------------------------------------
| id | team1name | team2name | team1score | team2score | weeknumber |
|------|-----------|-----------|-------------|------------|-------------|
| | | | | | |
| | | | | | |
-------------------------------------------------------------------------
The idea is, that table "Teams" is getting populated through another page that already works for me.
Every week there will be 13 new teams.
Table "Player" has to get populated from another page where the user gets the following options:
Name: Text-field
13 Checkboxes (those checkboxes need to store the value ID from table Teams)
The user is supposed to check 5 out of 13 teams (free choice) and the values of those checkboxes (the ID of the Teams ID) should be stored as T1, T2, T3, T4, T5 along with Name from text-field.
<form method="post" action="created.php" class="form-style-7">
<h2>Tilmeld en spiller </h2>
<ul>
<li>
<label for="name">Navn</label>
<input type="text" name="navn" value="<?php echo $navn;?>">
<span>Indtast spillerens navn</span>
</li>
<li>
<?php
$count = $rows = 0;
$sql = "SELECT * FROM Teams WHERE weeknumber='$WeekNumber' ORDER BY id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo '
<table class=""><tr><th colspan="3">Igangværende - Uge ' . $WeekNumber . '</th></tr>
<tr class="underhead"><td>#</td><td>Kamp</td><td>Mål</td></tr>
';
// Output af data
while($row = $result->fetch_assoc()) {
if ($rows % 1 == 0) {
$count++;
}
$rows++;
echo '<tr>';
echo '<td>';
echo '';
echo '<input type="hidden" name="ID" value="' . $row['id'] . '" >';
echo '<input class="btn" type="checkbox" name="" > ';
echo ' </form>';
echo '</td>';
echo '<td>' . $count . '</td>';
echo '<td>' . $row["team1name"]. ' - ' . $row["team2name"] . '</td>
</form>';
}
echo '</table>';
}
mysqli_close($conn);
?>
</li>
<li id="buttontilmeld">
<input type="submit" name="submit" value="Tilmeld" >
</li>
</ul>
</form>
Edit:
My created.php contains the following:
if (isset($_POST['navn']) && $_POST['navn'] != "") {
if (isset($_POST['navn'])) {
if ($_POST['name'] == "" || $_POST['T1'] == "" || $_POST['T2'] == "" || $_POST['T3'] == "" || $_POST['T4'] == "" || $_POST['T5'] == "") {
echo 'XX';
}
} else {
require 'config.php';
$conn = Connect();
$name = $conn->real_escape_string($_POST['navn']);
$match1 = $conn->real_escape_string($_POST['kamp1']);
$match2 = $conn->real_escape_string($_POST['kamp2']);
$match3 = $conn->real_escape_string($_POST['kamp3']);
$match4 = $conn->real_escape_string($_POST['kamp4']);
$match5 = $conn->real_escape_string($_POST['kamp5']);
$WeekNumber = date('W');
$query = "INSERT into Spillere (navn,kamp1,kamp2,kamp3,kamp4,kamp5,ugenummer) VALUES('" . $name . "','" . $match1 . "','" . $match2 . "','" . $match3 . "','" . $match4 . "','" . $match5 . "','" . $WeekNumber . "')";
$success = $conn->query($query);
if (!$success) {
die("Couldn't enter data: ".$conn->error);
echo "Thanks";
}
}
}
Where I need $_POST['TX']=="" to populate table Player T1 to T5.
The name attribute of the checkboxes are not set. PHP can't get any info about the checked checkboxes (unless you should use Jquery en do some other stuff). It's important to know that only checked checkboxes will be visible in the POST data.
Give your checkboxes a name, for example:
echo '<input class="btn" type="checkbox" name="team_checkbox_'.$row['id'].'"> ';
Then in the script created.php were your data is posted to you can catch these values by running the same SQL-query and trying
if(isset($_POST['team_checkbox_'.$row['id']])){
// $row['id'] is checked
}
Or just check all POST data:
$t = array();
foreach($_POST as $post_param_name => $value){
if(substr($post_param_name, 0, 14) == 'team_checkbox_'){
// substr($post_param_name, 14) is the ID of one of the checked groups
$t[] = substr($post_param_name, 14);
}
}
Now $t is an array containing the team ID's of the checkboxes. So you can save in your SQL: T1 = $t[1], ...
This answer should be reviewed, but the solution is in another answer here:
https://stackoverflow.com/a/47162431/1589379
Follow good coding standards
There's a lot of little problems with the code you posted. Ultimately, you should always be very careful to keep everything properly indented, and always use brackets {}, even if they're not needed.
Your original code:
while (...) {
if ($rows % 1 == 0)
$count++;
Should be:
while (...) {
if ($rows % 1 == 0) {
$count++;
}
Those adjustments would have made it much more clear that your code had two closing </form> tags, and no opening tags, within your while loop.
I have a mini game which is written in PHP. I want to build a highscore section for it. But I can't echo datas properly. My database
+--------+----------+----------------------------------+---------------------+------+------+
| UserID | Username | Password | EmailAddress | win | lost |
+--------+----------+----------------------------------+---------------------+------+------+
| 1 | utku | e10adc3949ba59abbe56e057f20f883e | utku#utku.com | 3 | 6 |
| 2 | utku2 | e10adc3949ba59abbe56e057f20f883e | utku#sda.com | 5 | 15 |
| 3 | utku3 | e10adc3949ba59abbe56e057f20f883e | sad | 0 | 0 |
+--------+----------+----------------------------------+---------------------+------+------+
I'm trying to echo them with this code (I found it in another question's topic)
<?php include "base.php"; ?>
<?
$query="SELECT Username,win,lost FROM users ORDER BY win";
$results = mysql_query($query);
while ($row = mysql_fetch_array($results)) {
echo '<tr>';
foreach($row as $field) {
echo '<td>' . htmlspecialchars($field) . '</td>';
}
echo '</tr><br>';
}
?>
It prints datas like this
utku3utku30000
utkuutku3366
utku2utku2551515
But I want to print them in this form
Username Win Lost
utku2 5 15
utku 3 6
utku3 0 0
How can I do it. I'm new on PHP
You should not use mysql_ as it is outdated and will be removed in future versions of php.
You should switch to mysqli_ or PDO. (Overview of the MySQL PHP drivers > Choosing an API)
Your problem is:
array mysql_fetch_array ( resource $result [, int $result_type = MYSQL_BOTH ] )
MYSQL_BOTH: [...]By using MYSQL_BOTH (default), you'll get an array with both associative and number indices.[...]
That's why you get each column twice.
A quick fix would be using MYSQL_NUM or MYSQL_ASSOC:
mysql_fetch_array($results, MYSQL_ASSOC)
You should not print tr, td without table tag. Also you did not added th. Can try this
echo '<table><tr><th>User Name</th><th>Win</th><th>Lost</th></tr>';
while ($row = mysql_fetch_array($results)) {
echo '<tr><td>'.$row['Username'].'</td><td>'.$row['win'].'</td><td>'.$row['lost'].'</td></tr>';
}
echo '</table>';
You don't need the foreach loop because the while loop is doing the recursive iteraction, that's why you have twice results:
while ($row = mysql_fetch_array($results)) {
echo '<tr>';
echo '<td>' . htmlspecialchars($field['Username']) . '</td>';
echo '<td>' . htmlspecialchars($field['win']) . '</td>';
echo '<td>' . htmlspecialchars($field['lost']) . '</td>';
echo '</tr><br>';
}
I have sql data result set having records as follows
id | name | hobbies
------------------------------------
1 | RAVI | PLAYING CRICKET
------------------------------------
1 | RAVI | LISTENING MUSIC
------------------------------------
2 | REENA | BADMINTON
------------------------------------
I am displaying this data in view by using html table.
Whereas my requirement is, I want to display as follows
id | name | hobbies
------------------------------------
1 | RAVI | PLAYING CRICKET
| | LISTENING MUSIC
------------------------------------
2 | REENA | BADMINTON
------------------------------------
meaning I want to display records with id 1 into one <td>
I am using php foreach loop to display result.
How can I achieve this?
My current code is as follows and is results into same as my first table whereas I want my view as in the second table.
<table class="table table-striped">
<tr >
<th>ID</th>
<th>Name</th>
<th>Hobbies</th>
</tr>
foreach($result as $row)
{
echo "<tr>
<td>".$row->id."</td>
<td>". $row->name."</td>
<td>". $row->hobbies."</td>
</tr>";
}
</table>
A quick way to approach this would be to modify your MySQL query to use GROUP_CONCAT(hobbies) to group all of a user's hobbies together. The query would look something like:
SELECT
id, name, GROUP_CONCAT(hobbies) AS hobbies
FROM
your_table
GROUP BY
id
This will group all of a user's hobbies in a comma-delimited list. To display it, you can use PHP's explode() and iterate over that:
foreach ($results as $row) {
echo '<tr>';
echo '<td>' . $row->id . '</td>';
echo '<td>' . $row->name . '</td>';
echo '<td>';
$hobbies = explode(',', $row->hobbies);
foreach ($hobbies as $hobby) {
// output each hobby and decorate/separate them however you'd like
echo $hobby . '<br />';
}
echo '</td>';
echo '</tr>';
}
If you don't want the inner loop (or the ending <br /> that will pad the hobbies), you can use str_replace() instead:
echo str_replace(',', '<br />', $result['hobbies']);
This post may help you: Concatenate many rows into a single text string?
Just replace the , with </br>.
in each iteration, you must save the last ID, in the next iteration you must check it whether its value has been changed or not. Something like this:
$res = $this->db->query('.....');
$last_ID = '';
foreach ($res->result() as $row) {
if ($row->id != $last_ID) {
if (strlen($last_ID)>0) echo '</td></tr>';
echo '<tr>';
echo '<td>$row->id</td>';
echo '<td>$row->name</td>';
echo '<td>$row->hobbies';
} else {
echo '<br />'.$row->hobbies;
}
$last_ID = $row->id;
}
if ($res->num_rows() > 0) echo '</td></tr>';
Supposing you have your row in $row var and the ID is $row["ID"]
?>
<td id="<php echo $row["id"]; ?>">
<?php echo $row["id"]; ?>
</td>
<?php
All i want to do is to store the user entered values to a database.There is a table and it has many text boxes where the user enters his input.Upon clicking submit button the values should be stored in the mysql table.
I'm getting some errors right now.I'm completely new to php,Below I've attached the code and screenshots.
//workingcode.php
<?php
$con = mysql_connect("localhost","tom","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
//select Database
mysql_select_db("sms", $con);
$result = mysql_query("SELECT * FROM dept_info", $con) or die(mysql_error());
echo "<table border='1'>";
echo '<tr> <th>Sl No</th> <th>USN</th> <th>Name</th><th>code1</th><th>code 2</th> <th>code 3</th> <th>code 4</th> <th>code 5</th>
<th>code 6</th> <th>code 7</th> <th>code 8</th></tr>';
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo $row['usn'];
echo "</td><td>";
echo $row['name'];
echo "</td>";
for ($i=1; $i <= 8; $i++) {
echo "<td> <input type='text' name='sl[{$row['usn']}][$i]' size='2' /> </td>" ;
}
echo '</tr>';
}
echo '
<form action = "insert.php" method = "post">
<div align="bottom">
<input type = "submit" align = "BOTTOM" value = "Proceed">
</div>
</form>
';
?>
I've linked this to an insert.php file as shown below..
<html>
<head>
<title>Marks Entry Results</title>
</head>
<body>
<h1>Student Marks Entry Results</h1>
<?php
$insertData = array();
foreach ($_POST['sl'] as $usn => $codes) {
foreach ($codes as $sl) {
$insertData[] = sprintf("('%s', %d)", mysqli_real_escape_string($usn), intval($sl));
}
}
$con = mysql_connect("localhost","tom","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
//select Database
mysql_select_db("sms", $con);
$query = "INSERT INTO mytable (usn, code1) VALUES\n" . join(",\n", $insertData);
$result = mysql_query($query);
if ($result) {
echo mysql_affected_rows()." Marks inserted into database.";
} else {
echo "An error has occurred. Not added.";
}
mysql_close();
?>
</body>
</html>
//The description of the table which i made for this is
/*mysql> desc mytable;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| usn | varchar(50) | NO | PRI | NULL | |
| code1 | int(2) | YES | | NULL | |
| code2 | int(2) | YES | | NULL | |
| code3 | int(2) | YES | | NULL | |
| code4 | int(2) | YES | | NULL | |
| code5 | int(2) | YES | | NULL | |
| code6 | int(2) | YES | | NULL | |
| code7 | int(2) | YES | | NULL | |
| code8 | int(2) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
9 rows in set (0.01 sec)
*/
The textboxes are not inside any form tag so when you hit submit the value of textboxes doesn't get post back to server...Try following.
echo '<form action = "insert.php" method = "post">';
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo $row['usn'];
echo "</td><td>";
echo $row['name'];
echo "</td>";
for ($i=1; $i <= 8; $i++) {
echo "<td> <input type='text' name='sl[{$row['usn']}][$i]' size='2' /> </td>" ;
}
echo '</tr>';
}
echo '
<div align="bottom">
<input type = "submit" align = "BOTTOM" value = "Proceed">
</div>
</form>';
//insert.php
<?php
$insertData = array();
foreach ($_POST['sl'] as $usn => $codes) {
foreach ($codes as $sl) {
$insertData[] = sprintf("('%s', %d)", mysqli_real_escape_string($usn), intval($sl));
}
}
$con = mysql_connect("localhost","tom","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
//select Database
mysql_select_db("sms", $con);
$query = "INSERT INTO mytable (usn, code1) VALUES\n" . join(",\n", $insertData);
$result = mysql_query($query);
if ($result) {
echo mysql_affected_rows()." Marks inserted into database.";
} else {
echo "An error has occurred. Not added.";
}
mysql_close();
?>