Jump to content
Sign in to follow this  
KT27

Nevar izvilkt no foreach loop atiecīgās rindas memberID ID numuru priekš sql UPDATE!

Recommended Posts

KT27
Posted (edited)

sveiki,

nepieciešama palīdzība,

nedarbojas UPDATE mysql ,  memberID iekš foreach(); nedarbojas korekti, apdeits tiek veikts tikai pedejam ierakstam,

varbut var kads palīdzēt kur esmu iestrēdzis un ko daru nepareizi, ir dropdowns (

Quote

echo '<td><form method="post" action=""><select name="bannerTitle" onchange="this.form.submit()">'.$op_bn.'</select></form</td>';

 

)

kas ņem no db datus un caur if($_post) taisa UPDATE members tabulai salīdzinot memberID, bet tas īstio tā nenotiek, man tādas aizdomas ka caur foreach izmet visus memberID

id numurus nevis attiecībās rintas id numuru ko vajag Updeitot..  beigu rezulātā, apdeits ir tikai pedejam ievietotam ierakstam members datubāzē..

netieku skaids..

 

jebkura palīdzība un padoms būtu lielisks!!!

 

šeit būs koda gabals:

 

 

 
Quote

 

<?php
error_reporting(E_ALL & ~E_NOTICE & ~E_USER_NOTICE);
ob_start();
session_start();
 
//database credentials
define('DBHOST','localhost');
define('DBUSER','root');
define('DBPASS','root');
define('DBNAME','dbname');
 
$db = new PDO('mysql:host='.DBHOST.';dbname='.DBNAME.';charset=utf8mb4', DBUSER, DBPASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>
 
<section>
<table>
<thead>
<tr>
<th>ID</th>
<th>Banner</th>
</tr>
</thead>
<tbody>
<?php
try {
$op_bn='<option disabled selected>Select Banner</option>';
$stmt = $db->prepare('SELECT * FROM banners');
$stmt->execute();
$result = $stmt->fetchAll();
$img_limk = '<img style="width:50px; height:50px;" src="../'.$row['bannerSlug'].'">';
foreach ($result as $row) {
$op_bn.= "<option value='" . $row['bannerSlug'] . "'>" . $row['bannerTitle'] . "</option>";
}

 

$stmt = $db->query('SELECT memberID, banner FROM members ORDER BY memberID DESC');
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $row) {
echo '<tr>';
echo '<td>'.$row['memberID'].'</td>';

 

################################### NEDARBOJAS KOREKTI ##############################
 
if(isset($_POST['bannerTitle'])){
$stmt = $db->prepare('UPDATE members SET banner=:banner WHERE memberID=:memberID');
$stmt->execute(array(
':banner' => $_POST['bannerTitle'],
':memberID' => $row['memberID']
));

 

$_SESSION['success'] = "Banner successfully added to member profile!";
header('Location: view_members.php');
exit;
}
 
echo '<td><form method="post" action=""><select name="bannerTitle" onchange="this.form.submit()">'.$op_bn.'</select></form</td>';

 

################################### NEDARBOJAS KOREKTI ##############################
echo '</tr>';
}

 

} catch(PDOException $e) {
echo $e->getMessage();
}
?>
</tbody>
</table>
</section>

 

 
datubāze:
 
Quote

CREATE TABLE `members` (
  `memberID` int(11) NOT NULL AUTO_INCREMENT,
  `banner` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`memberID`)
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=latin1;

 

 

Quote

CREATE TABLE `banners` (
  `bannerID` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `bannerTitle` varchar(255) DEFAULT NULL,
  `bannerSlug` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`bannerID`)
) ENGINE=MyISAM AUTO_INCREMENT=67 DEFAULT CHARSET=utf8;

 

Edited by KT27

Share this post


Link to post
Share on other sites
Mezavecis
2020.05.23. , 19:57, KT27 teica:
$stmt = $db->prepare('UPDATE members SET banner=:banner WHERE memberID=:memberID');
$stmt->execute(array(
':banner' => $_POST['bannerTitle'],
':memberID' => $row['memberID']

Derētu paskaidrot, kas šeit ir domāts un kas tas par array tiek padots uz SQL pieprasījumu.

  • Atbalstu 1

Share this post


Link to post
Share on other sites
Anonīms Alkoholiķis

Vai tiešām ir grūti apskatīties php dokumentācijā, kas ir PDO, ko tas ziemā ēd un kā to izmanto ar visiem piemēriem? 

 

Tā pat, kā uzdot php jautājumus phpistu forumā vai ja galva psšam nestrādā tad iet copypeistot kodu no stackoverflowa?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...