9 Replies - 481 Views - Last Post: 15 April 2020 - 08:39 AM Rate Topic: -----

#1 pythonhelp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 20-January 19

Updating multiple records

Posted 15 April 2020 - 12:22 AM

Hi Guys,

I am new to PHP/MYSQL. I am working on a web database project to record class attendance.
I have found this script which I have manipulated, however, I am not sure why it does not display the data. It may be something simple, I have checked over table names, field names, variable names to see that they match the database.

Can you help?

I will be focusing on Prepared Statements for SQL injections later.

<?php

$servername = "xxxx";
$username = "xxxx";
$password = "xxxxx";
$dbname = "xxxxxx";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql="SELECT * FROM attendance";
$result=mysql_query($sql);

// Count table rows
$count=mysql_num_rows($result);


?>


<html>
<head>
<title>Registers</title>
</head>
<body>


<?php include 'Navigation.php';?>

<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">

<table border="0" cellspacing="1" cellpadding="0" style="width: 1461px; height: 105px">

<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Last Name</strong></td>
<td align="center"><strong>First Name</strong></td>
<td align="center"><strong>Form</strong></td>
<td align="center"><strong>Year Group </strong></td>
<td align="center"><strong>Date</strong></td>
<td align="center"><strong>Attendance</strong></td>


</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td align="center"><? $Student_id[]=$rows['Student_id']; ?><? echo $rows['Student_id']; ?></td>
<td align="center"><? $firstname[]=$rows['firstname']; ?><? echo $rows['firstname']; ?></td>
<td align="center"><? $lastname[]=$rows['lastname']; ?><? echo $rows['lastname']; ?></td>
<td align="center"><? $Form_Group[]=$rows['Form_Group']; ?><? echo $rows['Form_Group']; ?></td>
<td align="center"><? $Year_Group[]=$rows['Year_Group']; ?><? echo $rows['Year_Group']; ?></td>
<td align="center"><? $Att_Date[]=$rows['Att_Date']; ?><? echo $rows['Att_Date']; ?></td>
<td align="center"><input name="Presence[]" type="text" id="Presence" value="<? echo $rows['Presence']; ?>"></td>
</tr>

<?php
}
?>

<tr>
<td colspan="7" align="center"><input type="submit" name="Submit" value="Submit Register"></td>
</tr>
</table>

</form>
</table>


<?php

// Check if button name "Submit" is active, do this
if($Submit){
for($i=0;$i<$count;$i++){
$sql1="UPDATE attendance SET Presence='$Presence[$i]' WHERE Student_id='$Student_id[$i]'";
$result1=mysql_query($sql1);
}
}

if($result1){
header("location:Registers.php");
}
mysql_close();
?>




Is This A Good Question/Topic? 0
  • +

Replies To: Updating multiple records

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,561
  • Joined: 08-June 10

Re: Updating multiple records

Posted 15 April 2020 - 12:31 AM

you have mixed mysql_* functions and mysqli_* functions, which are not compatible.
Was This Post Helpful? 0
  • +
  • -

#3 pythonhelp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 20-January 19

Re: Updating multiple records

Posted 15 April 2020 - 12:50 AM

Thank you, but there still seems to be an error somewhere, it displays the textboxes with the php code rather than the data and all other fields display no data.


<?php

$host="xxx"; // Host name
$username="xxx"; // Mysql username
$password="xxx"; // Mysql password
$db_name="xxxxx"; // Database name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$sql="SELECT * FROM attendance";
$result=mysql_query($sql);

// Count table rows
$count=mysql_num_rows($result);


?>


<html>
<head>
<title>Registers</title>
</head>
<body>


<?php include 'Navigation.php';?>

<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">

<table border="0" cellspacing="1" cellpadding="0" style="width: 1461px; height: 105px">

<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Last Name</strong></td>
<td align="center"><strong>First Name</strong></td>
<td align="center"><strong>Form</strong></td>
<td align="center"><strong>Year Group </strong></td>
<td align="center"><strong>Date</strong></td>
<td align="center"><strong>Attendance</strong></td>


</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td align="center"><? $Student_id[]=$rows['Student_id']; ?><? echo $rows['Student_id']; ?></td>
<td align="center"><? $firstname[]=$rows['firstname']; ?><? echo $rows['firstname']; ?></td>
<td align="center"><? $lastname[]=$rows['lastname']; ?><? echo $rows['lastname']; ?></td>
<td align="center"><? $Form_Group[]=$rows['Form_Group']; ?><? echo $rows['Form_Group']; ?></td>
<td align="center"><? $Year_Group[]=$rows['Year_Group']; ?><? echo $rows['Year_Group']; ?></td>
<td align="center"><? $Att_Date[]=$rows['Att_Date']; ?><? echo $rows['Att_Date']; ?></td>
<td align="center"><input name="Presence[]" type="text" id="Presence" value="<? echo $rows['Presence']; ?>"></td>
</tr>

<?php
}
?>

<tr>
<td colspan="7" align="center"><input type="submit" name="Submit" value="Submit Register"></td>
</tr>
</table>

</form>
</table>


<?php

// Check if button name "Submit" is active, do this
if($Submit){
for($i=0;$i<$count;$i++){
$sql1="UPDATE attendance SET Presence='$Presence[$i]' WHERE Student_id='$Student_id[$i]'";
$result1=mysql_query($sql1);
}
}

if($result1){
header("location:Registers.php");
}
mysql_close();
?>






Thank

This post has been edited by Dormilich: 15 April 2020 - 01:05 AM

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,561
  • Joined: 08-June 10

Re: Updating multiple records

Posted 15 April 2020 - 01:06 AM

then you're either not calling php through the server or you're not using the .php file extension.

You are aware that PHP 5 had its end-of-life over a year ago?

https://www.php.net/...ed-versions.php
Was This Post Helpful? 0
  • +
  • -

#5 pythonhelp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 20-January 19

Re: Updating multiple records

Posted 15 April 2020 - 01:22 AM

Thanks

I have checked the file extension and its a PHP File loaded through a PHP server.

How do I check which version the script is?

Can you recommend an alternative script I can use to achieve the same purpose?

This post has been edited by Dormilich: 15 April 2020 - 04:15 AM

Was This Post Helpful? 0
  • +
  • -

#6 Ornstein   User is offline

  • D.I.C Head

Reputation: 105
  • View blog
  • Posts: 216
  • Joined: 13-May 15

Re: Updating multiple records

Posted 15 April 2020 - 01:31 AM

You're using short tags (<?) in the HTML; I'm pretty sure they were deprecated somewhere along the way and/or only work if you've got short tags enabled (e.g. with the short_open_tag option).
Was This Post Helpful? 0
  • +
  • -

#7 pythonhelp   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 20-January 19

Re: Updating multiple records

Posted 15 April 2020 - 01:45 AM

Thank you, I added the <?php and it worked!

The next issue is, I update the value in the textbox, after clicking submit, it shows the original value and not the updated (which means the value is probably not being updated in the database?

This post has been edited by Dormilich: 15 April 2020 - 04:15 AM

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Updating multiple records

Posted 15 April 2020 - 01:58 AM

Note that, as already indicated, you should not be using the mysql extension, it is long dead/deprecated. Nor should you be using obsolete HTML attributes such as width, align, etc..

You need to find up-to date material to follow, whatever you are following is probably more than 10 years extinct.

And definitely do not embed user-supplied values directly into SQL, user parameterized queries.
Was This Post Helpful? 0
  • +
  • -

#9 Ornstein   User is offline

  • D.I.C Head

Reputation: 105
  • View blog
  • Posts: 216
  • Joined: 13-May 15

Re: Updating multiple records

Posted 15 April 2020 - 02:28 AM

I'm guessing that if you did check the database or refreshed the page, you'd see the updated values. It looks like your code fetches the data from the database and then does the update - so obviously you're seeing the pre-update data. You should probably do the update first.
Was This Post Helpful? 0
  • +
  • -

#10 benanamen   User is offline

  • D.I.C Regular

Reputation: 38
  • View blog
  • Posts: 250
  • Joined: 28-March 15

Re: Updating multiple records

Posted 15 April 2020 - 08:39 AM

Quote

I have found this script


This is the first problem. You really cant just go out and grab something you find out on the net. There is so much junk and bad code out there. If you don't know what your doing you will not be able to sort through it.

Additionally, we generally do not debug third party code, especially obsolete code. We are here to help you learn and debug code that YOU wrote.

Assuming you actually want to learn coding and not just "find" something that "works", I would suggest you start with the following PDO tutorial and learn basic CRUD operations. (Create, Read, Update, Delete)

If you get stuck with that come back here and we will be happy to help you out. :bigsmile:

Pdo Tutorial

This post has been edited by benanamen: 15 April 2020 - 08:41 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1