Jump to content


  • Posts

  • Joined

  • Last visited

Everything posted by exceedinglife

  1. This should be a simple task I am just not fully grasping laravel yet. I have my controllers view and models setup. I want to use my users.destroy route to delete my row in the db. But I want to do it a certain way. I want to have an alert show In my alert area on my page asking to confirm the deletion of a certain user. Im assuming I need to pass the user id in a session to an alert to confirm my delete on a delete button click. Click 1 button to open an alert on the top of my page if I click confirm it calls user.destroy. <div class="container"> <div class="row justify-content-center"> <div class="col-md-12"> <div class="card"> <div class="card-header"> <h4>View All Users</h4> @if(session()->get('success')) <div class="alert alert-success"> {{ session()->get('success') }} </div> @endif @if(session()->get('danger')) <div class="alert alert-danger"> {{ session()->get('danger') }} </div> @endif </div> <div class="card-body"> <div class="text-center my-2"> <a href="{{ route('register') }}" class="btn btn-primary">New User</a> </div> <div> <table class="table table-striped table-bordered"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Email</th> <th>Username</th> <th colspan="2">Actions</th> </tr> </thead> <tbody> @foreach($users as $user) <tr> <th>{{$user->id}}</th> <td>{{$user->name}}</td> <td>{{$user->email}}</td> <td>{{$user->username}}</td> <td class="text-center"> <a href="{{ route('users.show', $user->id) }}" class="btn btn-primary mr-3">Show</a> <a href="{{ route('users.edit', $user->id) }}" class="btn btn-info text-white ml-3">Edit</a> <a href="#" class="btn btn-danger">Delete</a> </td> </tr> @endforeach </tbody> </table> public function destroy($id) { User::find($id)->delete(); return redirect()->route('users.index')->with('success','User Deleted'); } Route::resource('users', 'UserController');
  2. I put the var dump in the store method and nothing happened If I insert a row manually then the row appears in my index and I get can get and show the row with the correct data in my show view. Only I cannot update and create it. I can also delete a record successfully like this $contact = new Contact([ 'first_name' => var_dump($request->get('first_name')),
  3. Ok I fixed job_title Same thing happens tho I get the error alert on the top of the page when i click the submit button on my create view my fields say 'The first name field is required.' And nothing added to db
  4. Hello everyone, I have a PHP Laravel CRUD application I made where I am using MVC style. I have controllers views and models. My database migration is made and my table in the database is made with php artisan migrate. I am using php 7.3 and laravel 5.8. On my create view I go to create a single object in my database and my errors are thrown saying nothing in text box (no input) If I comment out the errors then just I click my submit button and nothing happens nothing is entered into my db. I have looked at many different crud examples and I am not sure why my object isn’t being created. Here is what I have //view create @section('main') <section id="section-content" class="text-center"> <div class="container contentdiv rounded"> <div class="row"> <div class="col-md-12"> <div class="pb-2 mt-4 mb-2 border-bottom clearfix"> <h2>Create Contact</h2> </div> <div > <a class="btn btn-success" href="{{route('contacts.index')}}">Back</a> </div> </div> <!-- <div class="col-md-10 mx-auto"> @if($errors->any()) <div class="alert alert-danger"> <ul> @foreach($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div><br /> @endif </div> --> <div class="row"> <div class="col-md-10 mx-auto mt-3"> <form method="POST" action="{{ route('contacts.store') }}"> @csrf <div class="form-group row"> <label for="txtfn" class="col-sm-3"><b>First Name:</b></label> <div class="col-sm-9"> <input type="text" class="form-control" name="txtfn" id="txtfn"/> </div> </div> <div class="form-group row"> <label for="txtln" class="col-sm-3"><b>Last Name:</b></label> <div class="col-sm-9"> <input type="text" class="form-control" name="txtln" id="txtln"/> </div> </div> <div class="form-group row"> <label for="txtem" class="col-sm-3"><b>Email:</b></label> <div class="col-sm-9"> <input type="text" class="form-control" name="txtem" id="txtem"/> </div> </div> <button type="submit" class="btn btn-primary">Create Contact</button> </form> </div> </div> </div> </section> //controller namespace App\Http\Controllers; use App\Contact; use Illuminate\Http\Request; class ContactController extends Controller { public function store(Request $request) { $request->validate([ 'first_name' => 'required', 'last_name' => 'required', 'email' => 'required' ]); $contact = new Contact([ 'first_name' => $request->get('first_name'), 'last_name' => $request->get('last_name'), 'email' => $request->get('email'), 'job_title' => $request->get('job_title'), 'city' => $request->get('city'), 'country' => $request->get('country') ]); $contact->save(); return redirect('/contacts')->with('success', 'Contact saved!'); } public function index() { $contacts = Contact::all(); return view('contacts.index', compact('contacts')); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { return view('contacts.create'); } // model namespace App; use Illuminate\Database\Eloquent\Model; class Contact extends Model { protected $fillable = [ 'first_name', 'last_name', 'email', 'city', 'country', 'job-title' ]; } My env is setup correctly I just don’t get the not creating object.
  5. I have a shopping cart that holds an array or my objects. I have all my customer user details serialized with a form and jQuery. I would like to insert the user data I got from my user details form (maybe I use payer or payer_info? Object. Also I would like to insert my items to the paypal CreateOrder: actions.order.create({ I am guessing I do it like this? "item_list": { "items": [ { "name": "hat", "description": "Brown color hat", "quantity": "5", "price": "3", "tax": "0.01", "sku": "1", "currency": "USD" }, { "name": "handbag", "description": "Black color hand bag", "quantity": "1", OR maybe in This is what I have $fname = $_POST['txtFirstname']; $lname = $_POST['txtLastname']; $email = $_POST['txtEmail']; var totalPrice = <?php echo $newTotal; ?> paypal.Buttons({ createOrder: function(data, actions) { // setup transaction return actions.order.create({ payer: { name: }, purchase_units: [{ amount: { value: totalPrice } }] }); },
  6. solved with echo '<img src="img/' . $row_product_image['name'] . '" class="w-100" />';
  7. Hello everyone, I have a PHP shopping cart I am working on. I have all my pictures stored in a folder url directory of my project. In my SQL database I have an image table that holds all of the image names. When I load the picture names with my php It somehow adds some extra random characters to the directory path: /img/%7B$row_product_image[name]%7D 404 (Not Found) If I hardcode the image directory img/picturename.jpg It works. Here is what I have. <?php include_once "objects/database.php"; include_once "objects/product.php"; include_once "objects/product_images.php"; // object instances $database = new Database(); $db = $database->getConnection(); $product = new Product($db); $product_image = new ProductImage($db); $recordsPerPage = 1; while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { extract($row); echo '<div class="col-md-4 mb-2">'; echo '<div class="product-id d-none">{$id}</div>'; echo '<a href="product.php?id={$id}" class="product-link">'; $product_image->product_id = $pid; $stmt_product_image = $product_image->readFirst(); while($row_product_image = $stmt_product_image->fetch(PDO::FETCH_ASSOC)) { echo '<div class="mb-1">'; echo '<img src="img/{$row_product_image[name]}" class="w-100" />'; echo '</div>'; } echo '<div class="product-name mb-1">{$name}</div>'; echo '</a>'; echo '</div>'; } class ProductImage { private $pdoConn; private $table_name = "product_images"; public $id; public $product_id; public $name; public $timestamp; public function __construct($dbconn) { $this->pdoConn = $dbconn; } function readFirst() { // SELECT query $query = "SELECT id, pid, name " . "FROM " . $this->table_name . " " . "WHERE pid = ? " . "ORDER BY name DESC " . "LIMIT 0, 1"; // prepare query statement $stmt = $this->pdoConn->prepare($query); // sanitize $this->product_id=htmlspecialchars(strip_tags($this->product_id)); // bind variable as parameter $stmt->bindParam(1, $this->product_id); // execute query $stmt->execute(); // return values return $stmt; } } ?>
  8. I have 2 arrays one of the types of numbers that will be used and the 2nd array is how many times that number can be used. I have a letter that determines what kind of method will be used I need to figure out how many times I can use a certain number from an array to determine a letter+number The ‘number’ is what I have to make with all the available numbers I can use. If the number cannot be made I would like to just say number cant be made or anything but allow the program to move on. Here is what I have int[] picks = { 100, 50, 20, 10, 5, 1 }; int[] times = { 10, 10, 10, 10, 10, 10 }; string choice = Console.ReadLine(); else if (choice.Equals("D")) { Dispense(amt, billCounts); }
  9. Hello All I am working on a application where I have to represent money and display how many bills were used to make each amount. I can do this The only problem I am working on figuring out is recording the number of each bill that has been used and to subtract it for the current number. I was thinking of making a money class for 100s 50s 20s 10s 5s and 1s but I cant have each value bill with a specified number of how many. Having the value specificed I would have this : but each one of these has no value amount int hund = 10; int fif = 10; int twen = 10; int ten = 10; int five = 10; int one = 10; Or if I want a value amount I was thinking this But I cant determine a fixed number to use public class Money { public int HundBill { get; set; } public int FiftBill { get; set; } public int TwentBill { get; set; } public int TenBill { get; set; } public int FiveBill { get; set; } public int OneBill { get; set; } public Money() { HundredDollarBill = cash / 100; cash %= 100; FiftyDollarBill = cash / 50; cash %= 50; TwentyDollarBill = cash / 20; cash %= 20; TenDollarBill = cash / 10; cash %= 10; FiftyDollarBill = cash / 5; cash %= 5; OneDollarBill = cash / 1; cash %= 1; } I am getting the users input and Ill get a number and I want to minus the number of bills that were used to make that amount and to keep track.
  10. LLOOOLLLL I cant believe that is all it was My session at the top of the page i didnt realize i had that also thats all i needed to get rid of and its fixed
  11. Hello all, I have a php login project that I am almost finished with. I have users in a table and I can login with the users BUT when I click the login button I get Notice: session_start(): A session had already been started - ignoring in E:\xampp\htdocs\PHP_Login\index.php on line 53 Warning: Cannot modify header information - headers already sent by (output started at E:\xampp\htdocs\PHP_Login\index.php:53) in E:\xampp\htdocs\PHP_Login\index.php on line 60 When I click the refresh button I get what I am supposed to get and I am logged in to the dashboard. <?php error_reporting(E_ALL); ini_set("display_errors", "1"); // Initialize SESSION session_start(); // Check if logged in ifso sent to Welcome.php if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true) { header("Location: php/welcome.php"); exit; } // Include config mySQL require_once "php/config.php"; // Define all variables and initialize them as 'empty' $username = $password = ""; $usernameerror = $passworderror = ""; // Process form data when submitted if($_SERVER["REQUEST_METHOD"] == "POST") { // Check if username is empty. if(empty(trim($_POST["username"]))) { $usernameerror = "Please enter a username"; } else { $username = trim($_POST["username"]); } // Check if password is empty. if(empty(trim($_POST["password"]))) { $passworderror = "Please enter a password"; } else { $password = trim($_POST["password"]); } // Validate credentials. if(empty($usernameerror) && empty($passworderror)) { // Prepare a SELECT statement. $sql = "SELECT userid, name, username, password FROM users WHERE " . "username = :username"; if($stmt = $pdoConn->prepare($sql)) { // bind variables to the prepared statement as parameters $stmt->bindParam(":username", $param_username, PDO::PARAM_STR); // Set parameters $param_username = trim($_POST["username"]); // Attempt to execute prepared statement. if($stmt->execute()) { // Check if username exists if so check password. if($stmt->rowCount() == 1) { if($row = $stmt->fetch()) { $id = $row["userid"]; $username = $row["username"]; $password_hashed = $row["password"]; $name = $row["name"]; if(password_verify($password, $password_hashed)) { // Password correct start new session session_start(); // store data in SESSION variables $_SESSION["loggedin"] = true; $_SESSION["id"] = $id; $_SESSION["username"] = $username; $_SESSION["name"] = $name; //Redirect to welcome.php header("Location: php/welcome.php"); } else { // If password INCORRECT error msg $passworderror = "Password was <b>Incorrect!</b>"; } } } else { $usernameerror = "No account was found."; } } else { echo "Error something went wrong, incorrect execution "; } } // Close prepared stmt unset($stmt); } // Close connection unset($pdoConn); } ?>
  12. My page continuely just keeps spinning on my btn click. In the network tab it opens a new tab (my php) and it has a yellow circile for google chrome - Provisional headers are shown In my form the correct data is shown - in form data. So its getting the correct values. idk if this is anything. General Request URL: http://localhost/php_crud/update.php?id=13 Referrer Policy: no-referrer-when-downgrade
  13. looks like i set this up awhile back ; Common Values: ; E_ALL (Show all errors, warnings and notices including coding standards.) ; E_ALL & ~E_NOTICE (Show all errors, except for notices) ; E_ALL & ~E_NOTICE & ~E_STRICT (Show all errors, except for notices and coding standards warnings.) ; E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR (Show only errors) ; Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED ; Development Value: E_ALL ; Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT ; http://php.net/error-reporting error_reporting=E_ALL
  14. Ok I will try that with php.ini One of the errors I found was para_id and it was suppose to be param_id Another issue was $param_date = $date; and it suppose to be $param_date = $currentDate; I changed my button <input ...> to <button></button> My page just continues to load
  15. No error messages. My form submits and clears the data when its valid but the data isnt updated Form is <form action="<?php echo htmlspecialchars(basename($_SERVER["REQUEST_URI"])); ?>" method="post" class="rounded" id="formUpdate"> <div class="form-group row"> <label for="txtid" class="col-sm-3"><b>Id:</b></label> <div class="col-sm-9"> <input type="text" class="form-control" id="txtid" name="txtid" disabled value="<?php echo $row["id"]; ?>" /> </div> </div> <div class="form-group row"> <label for="txtname" class="col-sm-3"><b>Name:</b></label> <div class="col-sm-9"> <input type="text" class="form-control" id="txtname" name="txtname" value="<?php echo $row["name"]; ?>" /> </div> </div> <?php if(isset($nameerror)) { echo '<span id="error"><b>' . $nameerror . '</b></span>'; } ?> <div class="form-group row"> <label for="txtlang" class="col-sm-3"><b>Language:</b></label> <div class="col-sm-9"> <input type="text" class="form-control" id="txtlang" name="txtlang" value="<?php echo $row["language"]; ?>" /> </div> </div> <?php if(isset($langerror)) { echo '<span id="error"><b>' . $langerror . '</b></span>'; } ?> <div class="form-group row"> <label for="txtdate" class="col-sm-3"><b>Date:</b></label> <div class="col-sm-9"> <input type="text" class="form-control" id="txtdate" name="txtdate" disabled value="<?php echo $row["date"]; ?>" /> </div> </div> <?php if(isset($dateerror)) { echo '<span id="error"><b>' . $dateerror . '</b></span>'; } ?> <input type="hidden" name="id" value="<?php echo $id; ?>" /> <input type="submit" class="btn btn-lg btn-primary btn-block" name="submit" value="Update"/> <a href="index.php" class="btn btn-lg btn-danger btn-block" role="button">Cancel</a> </form>
  16. Hello all, I have another project I’m working on a php login and I am inserting a record in the db I’m checking the table to see if it exists or not so there can’t be 2 users with the same username but I am working with create the form submits but nothing is inserted. Also appreciate suggestions in code to make my php better. <?php //mySQL database config require_once "config.php"; // Define all variables and initialize them as 'empty' $name = $username = $password = $password2 = ""; $nameerror = $usernameerror = $passworderror = $password2error = ""; // Process data when the form is submitted. if($_SERVER["REQUEST_METHOD"] == "POST") { //Name check if(empty(trim($_POST["name"]))) { $nameerror = "Please enter a name."; } else { $name = trim($_POST["name"]); } // Validate 'Username' if(empty(trim($_POST["username"]))) { $usernameerror = "Please enter a Username."; } else { // Prepare a SELECT statement. $sql = "SELECT userid FROM users WHERE username = :username"; if($stmt = $pdoConn->prepare($sql)) { // Bind variables to prepared statement as parameters $stmt->bindParam(":username", $param_username, PDO::PARAM_STR); // Set parameters $param_username = trim($_POST["username"]); // Attempt to execute prepared statement if($stmt->execute()) { if($stmt->rowCount() == 1) { $usernameerror = "Username is already taken."; } else { $username = trim($_POST["username"]); } } else { echo "Something went wrong with SELECT, please try again later."; } } // Close $stmt unset($stmt); } // Validate Password if(empty(trim($_POST["password"]))) { $passworderror = "Please enter a password."; } else if (strlen(trim($_POST["password"])) < 6) { $passworderror = "Password must have at least 6 characters."; } else { $password = trim($_POST["password"]); } // Validate Confirm Password. if(empty(trim($_POST["password2"]))) { $password2error = "Please confirm your password"; } else { $pass2 = trim($_POST["password2"]); if(empty($password2error) && ($password != $pass2)) { $password2error = "Passwords <b>DID NOT</b> match."; } } //Check for inputs on form to continue. // Error checks or input checks. if(empty($name) && empty($username) && empty($password) && empty($password2)) { // Prepare SELECT statement $sql = "INSERT INTO users (name, username, password) " . "VALUES (:name, :username, :password)"; if($stmt = $pdoConn->prepare($sql)) { // Bind variables to prepared statement as parameters $stmt->bindParam(":name", $param_name, PDO::PARAM_STR); $stmt->bindParam(":username", $param_username, PDO::PARAM_STR); $stmt->bindParam(":password", $param_pass, PDO::PARAM_STR); // Set parameters $para_name = $name; $param_username = $username; $param_pass = password_hash($password, PASSWORD_DEFAULT); // attempt to execute the prepared Statement if($stmt->execute()){ header("Location: ../index.php"); } else { echo "Something went wrong with INSERT"; } } // Close Statement unset($stmt); } // Close connection unset($pdoConn); } ?>
  17. Hey all, I have a CRUD php project I am working on. I have insert working reading and deleteing. The only one I have left is update. My page submits the form but when the btn is clicked my page refreshes and does not update in the db. All the controls are cleared. Im assuming I’m missing something simple or just have a small mistake that is running past me, <?php error_reporting(E_ALL & ~E_NOTICE); // include config for Database require_once "php/config.php"; // Declare the variables that will be used. $name = $language = $datenow = ""; $nameerror = $langerror = $dateerror = ""; // Process form data when its submitted if(isset($_POST["id"]) && !empty($_POST["id"])) { // Get hidden input value. $id = $_POST["id"]; $input_name = trim($_POST["name"]); if(empty($input_name)) { $nameerror = "Name is required. "; } elseif (!filter_var($input_name, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))) { $nameerror = "Please enter a valid name. "; } else { //$namesafe = mysqli_real_escape_string($connection, $input_name); $name = $input_name; } // Validate language entered $input_lang = trim($_POST["language"]); if(empty($input_lang)) { $langerror = "Please enter a language. "; } else { $language = $input_lang; } // Get current Date and Time $currentDate = date("Y-m-d H:i:s"); // OR $datetimeobj = new DateTime(); $datetimeobj->format("Y-m-d H:i:s"); //if(empty($nameerror) && empty($langerror) && empty($dateerror)) { if($name != "" && $language != "") { $sql = "UPDATE users SET name=:name, language=:language, " . "date=:date WHERE id=:id"; error_log($sql); if($stmt = $pdoConnect->prepare($sql)) { $stmt->bindParam(":name", $param_name); $stmt->bindParam(":language", $param_lang); $stmt->bindParam(":date", $param_date); $stmt->bindParam(":id", $param_id); $para_name = $name; $param_lang = $lang; $param_date = $date; $param_id = $id; if($stmt->execute()) { header("location: index.php"); exit(); } else { echo "Something went wrong, try again later."; } } } else { $errormsg = '<div > 'All fields are required to continue</div>'; } unset($stmt); unset($pdoConnect); } else { if(isset($_GET["id"]) && !empty(trim($_GET["id"]))) { $id = trim($_GET["id"]); $sql = "SELECT * FROM users WHERE id = :id"; if($stmt = $pdoConnect->prepare($sql)) { $stmt->bindParam(":id", $param_id); $param_id = $id; if($stmt->execute()) { if($stmt->rowCount() == 1) { $row = $stmt->fetch(PDO::FETCH_ASSOC); //$id = $row["id"]; $name = $row["name"]; $language = $row["language"]; $userdate = $row["date"]; } else { header("location: error.php"); exit(); } } else { echo "Something went wrong with UPDATE, try again later."; } } // Close $stmt statement unset($stmt); // Close connection unset($pdoConnect); } else { // URL doesn't contain valid 'id' parameter. header("location: error.php"); exit(); } } ?>
  18. Thank you! That cleared up alot. So you would suggest that i use $sql = "SELECT id, name, language, date FROM users WHERE id = ?"; instead of $sql = "SELECT id, name, language, date FROM users WHERE id = :id"; I will look at how to set my default PDO with fetch(PDO::FETCH_ASSOC); What should I use instead of rowCount() Thanks again. One other thing. I am having a problem with my header() I execute my SQL create and delete they both work. But when I try to transfer to a new page my page just continually keeps loading. Here is my code /Full DELETE if(isset($_POST["id"]) && !empty($_POST["id"])) { require_once "php/config.php"; // Prepare a DELETE statement $sql = "DELETE FROM users WHERE id=:id"; if($stmt = $pdoConnect->prepare($sql)) { $stmt->bindParam(":id", $param_id); $param_id = trim($_POST["id"]); if($stmt->execute()) { header("location: index.php"); exit(); } else { echo "Something went wrong with DELETE."; } } // Close $stmt statement unset($stmt); // Close connections unset($pdoConnect); } else { // Check existence of 'id' parameter if(empty(trim($_GET["id"]))) { //URL doesn't contain parameter send ERROR header("location: error.php"); exit(); } } // Create php code $sql = "INSERT INTO users(name, language, date) VALUES " . "(:name, :language, :date)"; if($stmt = $pdoConnect->prepare($sql)) { // Bind variables to prepared statement as parameters. $stmt->bindParam(":name", $para_name); $stmt->bindParam(":language", $para_lang); $stmt->bindParam(":date", $para_date); // Set parameters $para_name = $name; $para_lang = $language; $para_date = $currentDate; // Attempt to execute prepared statement if($stmt->execute()) { // Determine if Success or Error header("Location:index.php"); exit(); } else { echo "Something went wrong with INSERT, please try again later."; }
  19. Is PDO one of the best phps to use these days? For doing prepared statements how is this for my example of code below. Anything that i should do differently? Can you all look at this. How do you think this is for my prepared statements. Someone said PDO PHP is the way PHP should be used now adays? Is the correct. Here is my prepared statement. Am I doing everything correctly or what should i change? Here is for 1 row. And what about : mysqli_real_escape_string($connection, $_POST["name"]); I thought doing this was safe. but prepared is used over this? I dont really understand. $sql = "SELECT * FROM users WHERE id = :id"; // config for SQL Prepare if($stmt = $pdoConnect->prepare($sql)) { $stmt->bindParam(":id", $param_id); $param_id = trim($_GET["id"]); if($stmt->execute()) { if($stmt->rowCount() == 1) { $row = $stmt->fetch(PDO::FETCH_ASSOC); $id = $row["id"]; $name = $row["name"]; $language = $row["language"]; $userdate = $row["date"]; //And here is multiple records $sql = "SELECT * FROM users"; if($result = $pdoConnect->query($sql)) { if($result->rowCount() > 0) { while ($row = $result->fetch()) {
  20. woowww I cant believe the issue i was having LOL now i got it working all successfully
  21. delete this else { $errormsg = "HERE is the other stopping try"; die(); } I was testing somethin
  22. <?php $nameerror = $twoerror = $errormsg = ""; $namesafe = $twosafe = ""; // PHP Procedural MYSQLi // connect to mysql database with phpmyadmin $servername = "localhost"; $username = "root"; $password = "password"; $dbname = "test"; $connection = new mysqli($servername, $username, $password, $dbname); //if(isset($_POST["submit"])) if($_SERVER["REQUEST_METHOD"] == "POST") { if(empty(trim($_POST["name"]))) { $nameerror = "Name is required"; } else { $namesafe = mysqli_real_escape_string($connection, $_POST["name"]); } if(empty(trim($_POST["two"]))) { $twoerror = "Two is required"; } else { $twosafe = mysqli_real_escape_string($connection, $_POST["two"]); } if($namesafe != "" && $twosafe != "") { // Check and see if EXISTS $sqlCheck = "SELECT name FROM tester WHERE name ='". $namesafe ."'"; $check = mysqli_query($connection, $sqlCheck); $numRows = mysqli_num_rows($check); if($numRows != 0) { $errormsg = '<div class="alert alert-danger alert-dismissible fade show" role="alert"> <button type="button" class="close" data-dismiss="alert" aria-label="Close" aria-hidden="true"> &times;</button>Name has <b>ALREADY</b> been <u>used</u>!<br>'. $namesafe .'</div>'; die("die"); } else { $errormsg = "HERE is the other stopping try"; die(); } $sqlInsert = "INSERT INTO tester(name, two) " . "VALUES('". $namesafe ."','". $twosafe ."')"; if(mysqli_query($connection, $sqlInsert)) { echo "Successfully entered."; } else { echo "NOT successful error: " . $sqlInsert . "<br>" . mysqli_error($connection); } } else { $errormsg = '<div class="alert alert-danger alert-dismissible fade show" role="alert"><button type="button" ' . 'class="close" data-dismiss="alert" aria-label="Close" aria-hidden="true">&times;</button>' . 'All fields are required to continue</div>'; } } //else { echo "ELSE on btn click "; } mysqli_close($connection); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>PHP BootStrap mysql Create</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- BootStrap 4 CDN CSS external link --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous"> <!-- Custom CSS Link --> <link rel="stylesheet" href="css/main.css" /> </head> <body> <?php if(!$connection) { die("Connection Failed! " . mysqli_connect_error()); } echo "Connected Successfully@!"; ?> <section class="text-align" id="section-content"> <div id="alertMessages" class="container rounded"></div> <div id="contentdiv" class="container rounded"> <form id="formtest" class="rounded" method="post" action=""> <!-- action="" --> <h3>PHP Create</h3> <?php if(isset($errormsg)) { echo $errormsg; } ?> <div> <div class="form-group"> <input type="text" class="form-control" id="txtName" name="name" required/> <label for="txtName">Name </label> <?php if(isset($nameerror)) { echo '<span class="error"><b>' . $nameerror . '</b></span>'; } ?> </div> <div> <input type="text" class="form-control" id="txttwo" name="two" required/> <label for="txttwo">Text Two </label> <?php if(isset($twoerror)) { echo '<span class="error"><b>' . $twoerror . '</b></span>'; } ?> </div> </div> <button type="submit" class="btn btn-lg btn-primary btn-block" name="submit">Click</button> </form> </div> </section> <!-- BootStrap 4 CDN JavaScript --> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script> <!--<script></script>--> </body> </html> This is my whole project 100%
  23. Ok the query is being run I am checking name to see if it is existing in the db and it is but I want it to die if it finds one So I am trying to figure out how i can make it die if it finds a row
  24. How come my script isnt stopping when i do that check and i have the name in the db I am just doing it for the fun of it and im trying to do without unique in database
  25. lol THANKS i forgot about required. Here is another question I have. I have a mysql db and I am running a check to see if a name is in the table. Can I stop the query if the name is in the able without having the database being unique. Here is the code in which i mean, if($namesafe != "" && $twosafe != "") { // Check and see if EXISTS $sqlCheck = "SELECT * FROM tester WHERE name ='". $namesafe ."'"; $check = mysqli_query($connection, $sqlCheck); $numRows = mysqli_num_rows($check); if($numRows != 0) { $errormsg = '<div>Name has <b>ALREADY</b> been <u>used</u>!<br>'. $namesafe .'</div>'; die(); }
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.