Citera mig om du vill att jag ska hitta till ditt svar.
airikr.me /device:desktop. Andra projekt: Keizai, Koroth & Serenum.
Ok? Visa gärna hur dina SQL-frågor (SQL query) ser ut just nu, så att vi lättare kan vägleda dig.
denna försöker jag med
<?php
// connect to the database
include("connect-db.php");
// creates the new/edit record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
function renderForm($product_code = '', $product_name = '', $product_desc = '', $product_img_name = '', $price = '', $error = '', $id = '')
{ ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>
<?php if ($id != '') { echo "Edit"; } else { echo "New"; } ?>
</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link type="text/css" media="all" rel="stylesheet" href="css/styla.css">
</head>
<body>
<h1><?php if ($id != '') { echo "Edit"; } else { echo "New"; } ?></h1>
<?php if ($error != '') {
echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error
. "</div>";
} ?>
<form action="" method="post">
<div>
<?php if ($id != '') { ?>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<p>ID: <?php echo $id; ?></p>
<?php } ?>
<strong>product_code: *</strong> <input type="text" name="product_code"
value="<?php echo $product_code; ?>"/><br/>
<strong>product_name: *</strong> <input type="text" name="product_name"
value="<?php echo $product_name; ?>"/><br/>
<strong>product_desc: *</strong> <input type="text" name="product_desc"
value="<?php echo $product_desc; ?>"/><br/>
<strong>product_img_name: *</strong> <input type="text" name="product_img_name"
value="<?php echo $product_img_name; ?>"/><br/>
<strong>price: *</strong> <input type="text" name="price"
value="<?php echo $price; ?>"/><br/>
<p>* required</p>
<input type="submit" name="submit" value="Submit" />
</div>
</form>
</body>
</html>
<?php }
/*
EDIT RECORD
*/
// if the 'id' variable is set in the URL, we know that we need to edit a record
if (isset($_GET['id']))
{
// if the form's submit button is clicked, we need to process the form
if (isset($_POST['submit']))
{
// make sure the 'id' in the URL is valid
if (is_numeric($_POST['id']))
{
// get variables from the URL/form
$id = $_POST['id'];
$product_code = htmlentities($_POST['product_code'], ENT_QUOTES);
$product_name = htmlentities($_POST['product_name'], ENT_QUOTES);
$product_desc = htmlentities($_POST['product_desc'], ENT_QUOTES);
$product_img_name = htmlentities($_POST['product_img_name'], ENT_QUOTES);
$price = htmlentities($_POST['price'], ENT_QUOTES);
// check that firstname and lastname are both not empty
if ($product_code = '' || $product_name = ''|| $product_desc = ''|| $product_img_name == ''|| $price = '')
{
// if they are empty, show an error message and display the form
$error = 'ERROR: Please fill in all required fields!';
renderForm($product_code, $product_name, $product_desc, $product_img_name, $price, $error, $id);
}
else
{
// if everything is fine, update the record in the database
if ($stmt = $mysqli->prepare("UPDATE products SET product_code = ?, product_name = ?, product_desc = ?, product_img_name = ?, price = ?
WHERE id=?"))
{
$stmt->bind_param("ssi", $product_code, $product_name, $product_desc, $product_img_name, $price, $id);
$stmt->execute();
$stmt->close();
}
// show an error message if the query has an error
else
{
echo "ERROR: could not prepare SQL statement.";
}
// redirect the user once the form is updated
header("Location: view.php");
}
}
// if the 'id' variable is not valid, show an error message
else
{
echo "Error!";
}
}
// if the form hasn't been submitted yet, get the info from the database and show the form
else
{
// make sure the 'id' value is valid
if (is_numeric($_GET['id']) && $_GET['id'] > 0)
{
// get 'id' from URL
$id = $_GET['id'];
// get the recod from the database
if($stmt = $mysqli->prepare("SELECT * FROM products WHERE id=?"))
{
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->bind_result($id, $product_code, $product_name, $product_desc, $product_img_name, $price);
$stmt->fetch();
// show the form
renderForm($product_code, $product_name, $product_desc, $product_img_name, $price, NULL, $id);
$stmt->close();
}
// show an error if the query has an error
else
{
echo "Error: could not prepare SQL statement";
}
}
// if the 'id' value is not valid, redirect the user back to the view.php page
else
{
header("Location: view.php");
}
}
}
/*
NEW RECORD
*/
// if the 'id' variable is not set in the URL, we must be creating a new record
else
{
// if the form's submit button is clicked, we need to process the form
if (isset($_POST['submit']))
{
// get the form data
$product_code = htmlentities($_POST['product_code'], ENT_QUOTES);
$product_name = htmlentities($_POST['product_name'], ENT_QUOTES);
$product_desc = htmlentities($_POST['product_desc'], ENT_QUOTES);
$product_img_name = htmlentities($_POST['product_img_name'], ENT_QUOTES);
$price = htmlentities($_POST['price'], ENT_QUOTES);
// check that firstname and lastname are both not empty
if ($product_code = '' || $product_name = '' || $product_desc = '' || $product_img_name == '' || $price = '' )
{
// if they are empty, show an error message and display the form
$error = 'ERROR: Please fill in all required fields!';
renderForm($product_code, $product_name, $product_desc, $product_img_name, $price, $error);
}
else
{
// insert the new record into the database
if ($stmt = $mysqli->prepare("INSERT products (product_code, product_name, product_desc, product_img_name, price) VALUES (?, ?)"))
{
$stmt->bind_param("ss", $product_code, $product_name, $product_desc, $product_img_name, $price);
$stmt->execute();
$stmt->close();
}
// show an error if the query has an error
else
{
echo "ERROR: Could not prepare SQL statement.";
}
// redirec the user
header("Location: view.php");
}
}
// if the form hasn't been submitted yet, show the form
else
{
renderForm();
}
}
// close the mysqli connection
$mysqli->close();
?>
@starwolf1
Lägg din kod inom taggen [code][/code] så formateras din text lite snyggare. Se nedan för exempel.
$stmt->bind_param("ssi", $product_code, $product_name, $product_desc, $product_img_name, $price, $id);
Raden ovan innehåller fel, du har antagligen modifierat ett befintigt skript med andra variabler. Första parametern "ssi" skall mest troligt vara "ssssii". Läs mer om bind_parm här.
- Igår Corsair Platform 6: För dig som inte nöjer dig med Ikea-skrivbord 9
- Igår Rykte: Switch 2 släpps i höst – OLED-variant dröjer 29
- Igår Iphones marknadsandel faller i USA 42
- 25 / 4 Airtec Pro Type1 – batteridrivet alternativ till tryckluft på burk 79
- 25 / 4 Nu stiger hårddiskpriserna med uppemot 10 procent 21
- Letar efter gammalt barnspel från tidigt 2000.7
- Rykte: Switch 2 släpps i höst – OLED-variant dröjer29
- Sur, Ledsen, Galen?! Skriv av er här!21520
- Okänd enhet i enhetshanteraren6
- Escape from Tarkov382
- Premiär! Månadens Drop på SweClockers63
- Corsair Platform 6: För dig som inte nöjer dig med Ikea-skrivbord9
- Tips på grafikkort15
- Speldator runt 12k - Funkar detta?1
- Quiz: Vad kan du om Inet?73
- Säljes ASUS ROG STRIX GeForce GTX 1080 Ti 11GB Gaming OC
- Säljes Gamingdator, 7800X3D, RTX 4080 Super
- Säljes Samsung Galaxy Tab S9 Wi-Fi
- Köpes 24 eller 27 tum minst 144hz
- Säljes MSI GeForce GTX 1070 GAMING X 8G
- Säljes Garderobsrensning, möss och keycaps
- Säljes Acer 4K 144Hz XB273KGP - Gaming Prylar - Aio
- Säljes Spelkoder säljes
- Köpes NUC - gärna Intel 8th gen (Iris) men allt intressant
- Skänkes i386 Laptop Skänkes!
- Ny caps lock-symbol i Windows förbryllar HP-användare10
- Corsair Platform 6: För dig som inte nöjer dig med Ikea-skrivbord9
- Rykte: Switch 2 släpps i höst – OLED-variant dröjer29
- NetonNet varnar om läckta kunduppgifter18
- Premiär på SweClockers! Månadens drop med gamingskärm hos Elgiganten63
- IT-bolag: Teknikstrul är största tidsboven idag38
- Därför blockerar Windows 11 24H2 Start‑menyhack39
- Iphones marknadsandel faller i USA42
- Konsumentverket granskar tio nätbutiker som lurat kunder18
- Airtec Pro Type1 – batteridrivet alternativ till tryckluft på burk79
Externa nyheter
Spelnyheter från FZ
- Xbox-försäljningen dök 31 % årets första kvartal igår
- PS Plus-ägare får vänta på Fallout 4-uppdateringen igår
- Spelhelgen knackar på – den kommer med spel! igår
- Switch 2-rykten – Kan släppas 2024, kontrollerna får magnetfästen igår
- People Can Fly har skrotat sitt "banbrytande" action-RPG Project Dagger igår