Search the Community
Showing results for tags 'regular expression'.
-
I wish to implement regular expressions as a way to check user input and so far i came up with code below. When i run my script, and input numbers for first and last names, they get posted successfully regardless of my regex and i get this error Notice: "Undefined index: last_name in......." and "Undefined index: first_name in.......". I was wondering what i may be doing wrong. <?php $errors = array(); // .....create database connection......// if (isset($_POST['insert'])) { $last_name = trim($_POST['last_name']); $first_name = trim($_POST['first_name']); // initialize flag $OK = false; // initialize prepared statement $stmt = $conn->stmt_init(); // create SQL $sql = 'INSERT INTO voter_tracking ( v_id, last_name, first_name) VALUES(?, ?)'; if ($stmt->prepare($sql)) { // bind parameters and execute statement $stmt->bind_param('iss', $_POST['v_id'], $_POST['last_name'], $_POST['first_name']); // execute and get number of affected rows $stmt->execute(); if ($stmt->affected_rows > 0) { $OK = true; } } // redirect if successful or display error if ($OK) { echo 'posted'; exit; } else { $error = $stmt->error; } } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Home</title> </head> <body> <div id="main"> <fieldset> <legend><h2>Add New Voter Record:</h2></legend> <?php if (isset($error)) { echo "<p class=\"warning\">Error: $error</p>"; } ?> <form id="form1" method="post" action=""> <p> <label for="last_name">Last Name:</label> <?php // Full Name must contain letters, dashes and spaces only and must start with upper case letter. if(preg_match("/^[A-Z][a-zA-Z -]+$/", $_POST["last_name"]) === 0) $errName = '<p class="errText">Last Name must be from letters, dashes, spaces and must not start with dash</p>';?> <input type="text" name="last_name" class="widebox" id="name" required aria-required="true"> </p> <p> <label for="first_name">First Name:</label> <?php // Full Name must contain letters, dashes and spaces only and must start with upper case letter. if(preg_match("/^[A-Z][a-zA-Z -]+$/", $_POST["first_name"]) === 0) $errName = '<p class="errText">Last Name must be from letters, dashes, spaces and must not start with dash</p>';?> <input type="text" name="first_name" class="widebox" required aria-required="true"> </p> <p> <input type="submit" name="insert" value="Insert New Entry" id="insert"> </p> </form> </fieldset> </div> </body> </html>
-
Good morning, I have a script and I'm trying to check whether it contains 1 character and whether it is between A and Z. preg_match() seems to be returning 0 and I'm not sure why. Here is my code: <?php $conn = mysqli_connect("localhost","root","","gaming") or die("Error: ".mysqli_error()); $qry = "SELECT game_id, release_date, game_desc, game_icon FROM games"; if(isset($_GET['letter'])) { /*if(strlen($_GET['letter']) == 1) { } else { header("Location: ?page=games"); }*/ $match = preg_match("/A-Z/", $_GET['letter']); echo $match; $qry .= " WHERE game_tag = '{$_GET['letter']}'"; } $sql = mysqli_query($conn, $qry) or die("Error: ".mysqli_error($conn)); $game_output = ""; while($row = mysqli_fetch_assoc($sql)) { $game_output .= "<div class='game_containers'>"; $game_output .= "<img src='images/game_icons/{$row['game_icon']}' alt='{$row['game_id']}' class='game_icon' />"; $game_output .= "<h3 class='game_header'>{$row['game_id']}</h3>"; $game_output .= "<p class='game_desc'>{$row['game_desc']}</p>"; $game_output .= "<hr />"; $game_output .= "Release Date: <span class='game_date'>{$row['release_date']}</span>"; $game_output .= "</div>"; } mysqli_close($conn); ?> When I echo $match, it always returns 0 even though $_GET['letter'] contains say 'M' or 'C' Any thoughts? Kind regards, L2c.
- 5 replies
-
- preg_match()
- regexp
-
(and 3 more)
Tagged with:
-
Hi All, I have a piece of code from php.net if (!ctype_alnum($username) || !preg_match('/^(?:[a-z0-9_-]|\.(?!\.))+$/iD', $userfile)) { die("Bad username/filename"); } I do not understand "/D" ! I know of /g, /i OR /m for multi line.. can someone please help me understanding this expression.